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

    Set the title on the Exchange Shell window.

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

    The new title.

    Rename-ExchangeShell -Title "Mailbox Query"

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

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

    # 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

Syslog Message Collection for OMS from sources that do not support the agent

How to Configure Message Forwarding on a Mailbox Level

IIS Client Certificate Revocation Check Disable