Windows Terminal dependent PowerShell prompt

Windows Terminal gets more and more my standard-tool to interact with PowerShell. Beneath all the cool built-in-features, it allows pretty fonts, characters and therefor a very unique prompt, supported by oh-my-posh and posh-git.

During the setup of those 2 great tools, i figured out that oh-my-posh doesnt work with the standard pwsh.exe prompt. Special characters dont get rendered correctly. So i found a way to decide which prompt to use depending on beeing started in Windows Terminal or not.

The magic differnec is that in Windows Terminal exists a variable called WSLENV with the value WT_SESSION:.

So in my profile i added this little piece of code.

if ($env:WSLENV -eq 'WT_SESSION:') {
    import-Module posh-git
    Import-Module oh-my-posh
    Set-Theme Agnoster
    Set-Prompt
}
else { 
    function prompt
    {
        $m = 30 # maximum prompt length
        $str = $pwd.Path
        if ($str.length -ge $m)
        {
            # reading from the end of the path.
            $str = "..." + $str.substring($str.length - $m + 4)
        }
        "C-"+"$str> "
    }
}

Now i have the correct prompt in native pwsh.exe and Windows Terminal.

Cheers / Roman