Change PS Prompt Based on vCenter Connection

The Prompt of a Different Color

In my work environment we are undertaking a major infrastructure upgrade.  We are doing a total refresh of all of the virtual environment, and with this we are also setting up a new vCenter.  With this it can get confusing with having multiple PowerShell ISE apps open and which vCenter I’m currently connected.  I wrote this out of a need to keep my scripting environments straight just by looking at the prompt.

Making a Splash of Color

Currently I have 4 different vCenter servers that I can connect to during this refresh project.  So I didn’t just want to have the prompt to change to the vCenter Server name as the names are very similar. Changing color of the prompt makes it’s easier to distinguish them from one another.

A Prompt of Many Colors
Color Changing Prompt

The Code for the Color

Add the code to your host profile (links: how to setup a powershell profile, revisiting powershell profile), and then just connect to vCenter and your prompt will update.  Obviously you will need to update the names to include your vCenter Server name, and change the colors as you choose.

Function prompt {
    $color = ""
    switch($global:DefaultVIServer.Name){
        "old-vCenterPrimaryDataCenter" {write-host ("PS [$global:DefaultVIServer] $PWD>") -nonewline -foregroundcolor "green"; return " "}
        "old-vCenterAlternateDataCenter" {write-host ("PS [$global:DefaultVIServer] $PWD>") -nonewline -foregroundcolor "magenta"; return " "}
        "new-vCenterPrimaryDataCenter" {write-host ("PS [$global:DefaultVIServer] $PWD>") -nonewline -foregroundcolor "yellow"; return " "}
        "new-vCenterAlternateDataCenter" {write-host ("PS [$global:DefaultVIServer] $PWD>") -nonewline -foregroundcolor "cyan"; return " "}
        "" {write-host ("PS [not connected] $PWD>") -nonewline -foregroundcolor "white"; return " "}
    }
}

This small bit of code has already made an impact on my day to day operation.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.