After some searching around, it seems like you're right that there is no built-in hook to redefine the [computername]: tag [computername]: with a preview.
Fortunately, I have a hacking solution that might work for you!
To get the color, we can just use Write-Host . Write-Host output from the prompt function will be completely left, and that is what we want. Unfortunately, the [computername]: tag [computername]: is inserted by default immediately after that. This leads to the fact that the computer name is duplicated in the invitation, once with color and once without it.
We get around this by returning a string containing backspace characters, so non-color [computername]: will be overwritten. This is a normal query string, usually the current path.
Finally, in the case of a short line of the query string and does not completely overwrite the non-color [computername]: tag [computername]: we need to do some final cleanup by adding space characters. However, this may push the caret, so we need to add extra back spaces to get the caret back to the corrent position.
All use this on your remote computer:
# put your logic here for getting prompt color by machine name function GetMachineColor($computerName) { [ConsoleColor]::Green } function GetComputerName { # if you want FQDN $ipProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties() "{0}.{1}" -f $ipProperties.HostName, $ipProperties.DomainName # if you want host name only # $env:computername } function prompt { $cn = GetComputerName # write computer name with color Write-Host "[${cn}]: " -Fore (GetMachineColor $cn) -NoNew # generate regular prompt you would be showing $defaultPrompt = "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " # generate backspaces to cover [computername]: pre-prompt printed by powershell $backspaces = "`b" * ($cn.Length + 4) # compute how much extra, if any, needs to be cleaned up at the end $remainingChars = [Math]::Max(($cn.Length + 4) - $defaultPrompt.Length, 0) $tail = (" " * $remainingChars) + ("`b" * $remainingChars) "${backspaces}${defaultPrompt}${tail}" }
latkin
source share