Microsoft Virtualization Discussions

Powershell Prompt

bsti
3,360 Views

I use the Powershell Tookit a LOT, to the point now where I do most of my controller management via Powershell. One issue I have doing this though is sometimes I'm not sure what controller I'm connected to. I wrote the following code blurb to help me always know what controller I'm connected to:

Edit your Powershell profile file ([my documents]\WindowsPowershell\profile.ps1). If this file does not exist, create it, then add this code:

function prompt
{
$label = "Not Connected"
$color = "gray"

if ( (Get-Variable | ? { $_.Name -ieq "currentNaController" }) )
{
if ( $global:currentNaController )
{
$label = $global:currentNaController
$color = "Green"
}
}

Write-Host ("[$label]") -ForegroundColor $color -NoNewline
return (" " + (Get-Location) + "> ");
}

When not connected, you see this:

When Connected, you get this:

I thought someone might find this handy.

2 REPLIES 2

timothyn
3,361 Views

Using the toolkit alot: cool

Using the toolkit enough to change your default prompt: priceless

Thanks for the great example!

david_ovenden
3,360 Views

I have found this handy, but I had trouble getting this to work in Powershell 3.0, but I got it working again by excluding the Get-Variable if statement;

function prompt
{
$label = "Not Connected"
$color = "gray"


if ( $global:currentNaController )
{
$label = $global:currentNaController
$color = "Green"
}

Write-Host ("[$label]") -ForegroundColor $color -NoNewline
return (" " + (Get-Location) + "> ");
}

Hopefully this will save others some pain

Public