Skip to main content
9 of 9
edited tags
Dharman Mod
  • 33.1k
  • 14
  • 170
  • 267

Proper syntax highlighting for PowerShell

Tagging a fenced code block with powershell:

```powershell
Write-Output "Hello, $Env:USERNAME"
```

produces no syntax highlighting:

Write-Output "Hello, $Env:USERNAME"

PowerShell snippet with no highlighting

This behavior holds for other common aliases for PowerShell - ps, ps1, etc.

You can trick it into the default highlighter by using lang-powershell or the sh highlighter using sh (as is mentioned in other questions ):

powershell snippet with crude highlighting

which is better than nothing, but is imperfect; note now the environment variable within the interpolated string is not properly highlighted as a variable. Compare vs. the standard syntax highlighter built into Windows:

enter image description here

So there are really two requests here:

  1. At least offer the default syntax highlighter on the powershell and ps names. For example, the block below uses the cs name, not the verbose lang-cs. I didn't even know about the need for the lang- prefix until I started researching this feature request, since I'd always been able to discover the short-form by guessing! The lang- prefix is non-discoverable and you have to go to documentation to learn about it, I've never seen it in any other Markdown implementation of fenced-code-blocks. It's not even mentioned in the formatting-help sidebar. Using lang-default or sh are non-discoverable workarounds that shouldn't be necessary.

    Console.WriteLine($"Hello, {Environment.UserName}.");
    
  2. Ideally, a proper syntax highlighter for PowerShell that understands the language fully, including its string-interpolation syntax. Note how the C# snippet above gets proper $"string interpolation"

Related questions:

Pxtl
  • 956
  • 4
  • 4