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.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
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.
[cc lang="PowerShell"]
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) + "> ")
}
[/cc]
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.
1 thought on “Domain Controller PowerShell Prompt”
Comments are closed.