Skip to main content
6 of 9
add related SO feature requests
Pxtl
  • 956
  • 4
  • 4

Proper syntax highlighting for Powershell

Note: This is a feature-request and not a request for a workaround - I'm aware of the workarounds and use them already. I mentioned that in the title but I guess that's bad form and was edited out?

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

(titles are paraphrased)

bash syntax highlighting as default language for powershell-tagged posts

Syntax highlighting: Use "lang-bash" (or similar) as default language for tags [powershell] and [powershell-core] until PowerShell is fully supported

This question is very informative about root-cause, but primarily focuses on the stackexchange tagging behavior and not fenced code blocks. Under that mechanism, using a powershell tag (but not a powershell-core tag) on the post does cause the lang-default, which is related to my request (1). However, as shown above that does not work for fenced code blocks. This is important since Powershell is often involved answer to various Windows admin tasks on SuperUser and ServerFault that may not be tagged with Powershell.

Enable automatic and on-demand syntax highlighting for PowerShell

Enable automatic and on-demand syntax highlighting for PowerShell (updated for highlight.js)

Functionally the same as above but includes more technical background. In particularly useful as it explains why the SE team is hesitant to add more languages (they have a performance load). This is used to explain why syntax highlighting for Julia was deferred.

However, PowerShell is an order-of-magnitude more questioned than Julia, and even now measures beyond Bash:

https://insights.stackoverflow.com/trends?tags=powershell%2Clisp%2Cperl%2Cbash%2Ccmd%2Cscheme%2Cgo%2Cjulia

graph of language popularity showing Powershell

Pxtl
  • 956
  • 4
  • 4