11

I mainly answer PHP questions where syntax highlighting has always worked for me with just indentation. I've just started using PowerShell and have asked a couple of questions and syntax highlighting seems to be wonky:

. "$PSScriptRoot\third.ps1"
#get $Current  = second.ps1
#get $Caller   = first.ps1 
#get $Original = first.ps1

$wc = new-object System.Net.WebClient
$wc.Headers.Add("user-agent", "PowerShell Script")
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$action = [System.IO.Path]::GetFileNameWithoutExtension($myInvocation.MyCommand.Name)

Wouldn't even some default syntax highlighting get one of the most common comment # styles right?

mklement0 states in Get original script name included script name and possibly others that using ```sh is a hack to get it to work:

    . "$PSScriptRoot\third.ps1"
    #get $Current  = second.ps1
    #get $Caller   = first.ps1 
    #get $Original = first.ps1

    $wc = new-object System.Net.WebClient
    $wc.Headers.Add("user-agent", "PowerShell Script")
    $wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
    
    $action = [System.IO.Path]::GetFileNameWithoutExtension($myInvocation.MyCommand.Name)
5
  • 1
    the [powershell] has syntax highlighting set to "default" (scroll to the very bottom of the page). So, there isn't a powershell specific highlighting.
    – VLAZ
    Commented Jun 11, 2021 at 16:09
  • 2
    Using code fences (```) with an explicit language (```sh) over code blocks (indenting by 4 spaces) has been recommend since the markdown overall anyway; so if you get in the habit of doing so you'll likely have a better experience.
    – Thom A
    Commented Jun 11, 2021 at 16:13
  • 5
    support this FR: meta.stackexchange.com/questions/355370/…
    – rene
    Commented Jun 11, 2021 at 17:03
  • Unless my 4 beers have totally F' ed me, it's happening with PHP mix HTML also stackoverflow.com/questions/67943395/… Commented Jun 11, 2021 at 21:35
  • 2
    The canonical is What is syntax highlighting and how does it work?. PowerShell is not mentioned at all in the list of supported languages ("PowerShell is not supported by the Stack Exchange version of highlight.js."). At a minimum, the most commonly used cmdlets (and their aliases) should be supported, but they just look weird when trying to use Bash or similar Commented Jun 12, 2021 at 0:05

2 Answers 2

7

Can't shed any light on the why, but the highlighter isn't even defaulting to anything here, it's just doing nothing. Setting the block manually to lang-default has seemingly better results:

. "$PSScriptRoot\third.ps1"
#get $Current  = second.ps1
#get $Caller   = first.ps1 
#get $Original = first.ps1

$wc = new-object System.Net.WebClient
$wc.Headers.Add("user-agent", "PowerShell Script")
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$action = [System.IO.Path]::GetFileNameWithoutExtension($myInvocation.MyCommand.Name)

So to answer your question

Wouldn't even some default syntax highlighting get one of the most common comment # styles right?

The answer is actually yes, the highlighter just isn't defaulting to anything here for some reason.


Also, calling the triple backticks method (```sh) a "hack" is a little weird... It's an officially supported way of specifying the highlighting language. In this case, you're specifying the tag , which tells the highlighter to use the language code specified by that tag.

In this case, is set to lang-sh, so it gives you the same results as if you set the block to use lang-sh manually (note that this language id happens to be synonymous with lang-bashlang-zsh):

. "$PSScriptRoot\third.ps1"
#get $Current  = second.ps1
#get $Caller   = first.ps1 
#get $Original = first.ps1

$wc = new-object System.Net.WebClient
$wc.Headers.Add("user-agent", "PowerShell Script")
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$action = [System.IO.Path]::GetFileNameWithoutExtension($myInvocation.MyCommand.Name)
11
  • 2
    Thanks. I call it a "hack" because PowerShell syntax is not shell, bash or zsh. I will test with lang-c and see what it does. And would it not be a bug to not default to anything? Commented Jun 11, 2021 at 16:40
  • 2
    That makes sense; it's disappointing that some languages just aren't supported. At least this is a case where what's available is seemingly close-ish.
    – zcoop98
    Commented Jun 11, 2021 at 16:42
  • It looks like it does have some colouring based on the fact that some of the text is syntax highlighted here
    – Thom A
    Commented Jun 11, 2021 at 16:50
  • @Larnu It does look like it worked in that case, even if it's still using the bash highlighter. If I had to guess (since they OP didn't share a link), the no-highlight that OP saw probably is due to tags on the question, since that's been known to affect the applied highlighter. I've forgotten the specifics of that though.
    – zcoop98
    Commented Jun 11, 2021 at 16:55
  • Seems like it's hit an miss, @zcoop98 . For the OP's code, it doesn't work at all: Sandbox attempt
    – Thom A
    Commented Jun 11, 2021 at 17:03
  • 3
    I think you know this but just in case: meta.stackexchange.com/questions/355370
    – rene
    Commented Jun 11, 2021 at 17:05
  • @Larnu Let me clarify (though you might be spot on that I'm wrong about this case), I dug up the post I was thinking of. This post by animuson explains how tags interact with the highlighter. "[W]e revert to "default" if there are two tags on a question that have different highlighters set."
    – zcoop98
    Commented Jun 11, 2021 at 17:24
  • @Larnu In your SO answer that you shared, that question has a 4 tags, and exactly one of them has a language set: [powershell], set to "default". If OP's answer, on the other hand, was on a question with >1 highlighted tag, it may have failed to highlight altogether (which I've definitely seen in the wild before).
    – zcoop98
    Commented Jun 11, 2021 at 17:25
  • 1
    In all cases I use ```powershell though @zcoop98 , so that statement shouldn't apply.
    – Thom A
    Commented Jun 11, 2021 at 18:35
  • @Larnu Gotcha! I definitely missed that detail on your's.
    – zcoop98
    Commented Jun 11, 2021 at 20:05
  • Even sh and powershell are not good. For example, highlighting Get-date on a line by itself with either option will see the - as a subtraction operator and then highlight the Get and date differently. Commented Oct 10, 2023 at 20:49
4

There is no great option for powershell right now.

For example, with lang-powershell:

Get-date

Or with ```sh

Get-date

Both cases see the - as a subtraction operator and then show the words "Get" and "date" in different colors. The third case doesn't do anything at all. Yet clearly this is one call to the Get-date cmdlet.

There's also ```powershell

Get-date

which does nothing at all, and ```lang-default

Get-date

which at least allows some kind of detection to work but still gets it wrong.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .