Domain Controller PowerShell Prompt

The other day I passed on a tweet I came across about creating a PowerShell prompt that displayed the domain controller that authenticated you. The original post was in a NetApp forum. Later I realized what was posted was something specific to NetApp’s PowerShell Toolkit. But you can use the same idea in a regular PowerShell session using the %LOGONSERVER% system environmental variable.

In PowerShell we would retrieve the variable with $env:logonserver. In a workgroup, or if for some reason you didn’t authenticate to a domain controller, this value should be the same as the computername. So I tweaked the original idea.

function prompt {

#check and see if logon server is the same as the computername
if ( $env:logonserver -ne "\\$env:computername" ) {
#strip off the \\
$label = ($env:logonserver).Substring(2)
$color = "Green"
}
else {
$label = "Not Connected"
$color = "gray"
}
 
Write-Host ("[$label]") -ForegroundColor $color -NoNewline
Write (" PS " + (Get-Location) + "> ")
}

Within the prompt function is a simple If statement that defines variables for label and color. One limitation with what I have so far is that if you run this on a domain controller it will show as not connected. But I’ll leave handling that scenario to you.

Download dcprompt.ps1.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

This entry was posted in PowerShell and tagged , , . Bookmark the permalink.

One Response to Domain Controller PowerShell Prompt

  1. Pingback: Domain Controller PowerShell Prompt - The Lonely Administrator - Powershell.com – Powershell Scripts, Tips and Resources

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>