Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Domain Controller PowerShell Prompt

Posted on November 15, 2011

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.

[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.

Share this:

  • Print (Opens in new window) Print

Like this:

Like Loading…

Related

1 thought on “Domain Controller PowerShell Prompt”

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

Comments are closed.

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2026 The Lonely Administrator | Powered by SuperbThemes!
%d