Changing the Exchange Management Shell window title

Although there are advantages in working with Powershell ISE, there may be times when Powershell is a better approach. Take Exchange for example. You may use the exchange commands by connecting to Exchange and using implicit remoting or use the remoteexchange script.

For those of us that are using the Exchange Management Shell, I believe that it would be helpful to be able to update the title of the window in order to reflect the use of that particular prompt.

Yesterday I put together a function that allows us to set the title of the window. This function is part of my CPolydorou.Exchange module but since that module has not yet been uploaded to the Powershell Gallery, here's the code:

Function Rename-ExchangeShell
{
    <#
    .SYNOPSIS

    Set the title on the Exchange Shell window.
    .DESCRIPTION

    Set the title on the Exchange Shell window.
    .PARAMETER Title

    The new title.
    .EXAMPLE

    Rename-ExchangeShell -Title "Mailbox Query"

    This command will rename the current Exchange Shell to "Mailbox Query"
    #>

    Param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Title
    )

    # Form the prompt function as a string
    $promptFunction = 'Function Global:Prompt{ $host.UI.RawUI.WindowTitle = "' + $Title + '"; Write-Host "[PS] " -NoNewLine -ForegroundColor Yellow; Write-Host (Get-Location).Path -NoNewLine; ">"}'

    # Create a scriptblock from that string
    $sb = [scriptblock]::Create($promptFunction)

    # Invoke the scriptblock
    Invoke-Command -ScriptBlock $sb
}

A few words about the code... We are constructing the prompt function as a string and then we create a scriptblock from that string. We then invoke the scriptblock and our prompt function is globally available.

The reason we are using the prompt function and not the "$host.UI.RawUI.WindowTitle" is that the Exchange prompt function will override it.

Popular posts from this blog

Domain Controller Machine Password Reset

Configuring a Certificate on Exchange Receive Connector

Running Multiple NGINX Ingress Controllers in AKS