Thursday, April 25, 2024
HomePowershellAltering the Look of the Immediate – SID-500.COM

Altering the Look of the Immediate – SID-500.COM


PowerShell

PowerShell comes with a typical immediate whose configuration is saved within the immediate operate. The excellent news is that we’re capable of change this immediate operate to go well with our wants. On this weblog submit, I’ll present you how one can change this immediate and I gives you some examples.

Topic

We’re investigating this topic:

And we’re additionally investigating the operate which is said to this default immediate:

(Get-Merchandise operate:immediate).ScriptBlock

That is the default immediate. For customizing, we have to change this default immediate.

Altering the Look of the Immediate

Listed below are 4 examples you need to use in your immediate or for additional customization.

Exhibiting the Date

operate immediate {

    "$(Get-Date)> "

}

Default Immediate + Computername

operate immediate {

    "PS [$env:COMPUTERNAME]> "

}

If PowerShell is began as administrator, then present [Administrator]

operate immediate {
    $id = [Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = [Security.Principal.WindowsPrincipal] $id
    $adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
  
    $(if (Take a look at-Path variable:/PSDebugContext) { '[DBG]: ' }
      elseif($principal.IsInRole($adminRole)) { "[ADMINISTRATOR]: " }
      else { '' }
    ) + 'PS ' + $(Get-Location) +
      $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
  }

Exhibiting the Username

operate immediate {

  "PS [$env:USERNAME]> "

}

I hope I used to be capable of spark some concepts on how one can change the immediate of your PowerShell session.

Extra about Prompts right here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_prompts?view=powershell-7.2#:~:textual content=Thepercent20Promptpercent20functionpercent20determinespercent20the,definingpercent20yourpercent20ownpercent20Promptpercent20function.&textual content=Thepercent20Promptpercent20functionpercent20mustpercent20return,ispercent20formattedpercent20aspercent20apercent20string.

In case you are questioning how one can completely implement your customized immediate take a look at my article about PowerShell Profiles: The best way to create PowerShell Profiles

Printed by Patrick Gruenauer

Microsoft MVP on PowerShell [2018-2023], IT-Coach, IT-Guide, MCSE: Cloud Platform and Infrastructure, Cisco Licensed Academy Teacher.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments