Categories


Authors

A proper PowerShell syntax highlighter

A big part of my job in cybersecurity involves writing reports and big parts of my personality are making things as easy and intuitive as possible for others, constant improvement, and “the little things” so I’m constantly developing my report templates to make them easier and easier to read. (This is why I ended up creating and posting a VBA macro to bulk insert files as objects into Word documents.)

These reports contain some code which naturally need to be read differently (or not at all, depending on the audience) so, to make these parts stand out, I recreated StackOverflow’s styling for pre / code elements which did help.

One day when I was bored, I decided to recreate the styling of the Windows PowerShell console / shell for the PS commands and I found that it looked much better.

Each version can be seen in these screenshots below:

I searched for a PowerShell syntax highlighter that I could use when writing my reports but all I found were ones that either simply didn’t work or ones that kinda worked but looked nothing like the real Windows PowerShell console / shell.

So, in yet another episode of me creating things that previously didn’t exist on the web, I decided to take Prism.js’ syntax highlighter and improve it by making the following changes:

  • Match colours to actual PowerShell console.

  • Fix matching / highlighting of custom functions.

  • Fix matching / highlighting of parameters.

  • Fix matching / highlighting of operator =.

  • Fix matching / highlighting of global and environment variables.

  • Add handling of double quotation marks and apostrophes in strings.

  • Fix matching / highlighting of commas in arguments (piggiebacked the string pattern for this).

  • Fix matching / highlighting of ++.

I have made it available to use at https://mythofechelon.co.uk/software/powershell-syntax-highlighter

Below is a brief demo using some PowerShell code from my script Update-hMailServerCertificate.

Great YouTube channels

How to work around Windows' restricted characters