OK the last couple of items have been long. I promise a short one here. Windows PowerShell doesn’t provide multiline comments. Multiline comments come in handy when you need to comment out multiple lines in a script file. However there is a reasonable workaround. Use a here string. A here string allows you to enter multiple lines of text and prevent PowerShell from interpreting commands. However the extent of PowerShell’s interpretation depends on which type of here string you use. For instance, in double quoted here strings, PowerShell expands variables and also execute subexpressions. This is an example of a double quoted here string e.g.:
PS C:\> @"
System.Diagnostics.Process (audiodg) System.Diagnostics.Process (csrss) …
However a single quoted here string doesn’t do this:
PS C:\> @’
Use the single quoted here string to comment out lines of script since it evaluates nothing in the contained string. Just note, the here string is an expression so if you do nothing more, the whole string will be emitted to the console. Usually you don’t want that when you are commenting out code. All you need to do is cast the string to [void] (or redirect the string to $null):
"Getting process info"
get-process | select Name, Id
"Killing all vd processes"
stop-process -name vd*
This will effectively comment out those lines of script. NOTE: There are a couple of gotchas to be aware of with here strings. There can be *NO* whitespace after the initial @’ character sequence. If there is one single space after this sequence you will get this cryptic error:
Unrecognized token in source text.
At C:\Temp\foo.ps1:1 char:1
+ @ <<<< ‘
The other gotcha is that the closing ‘@ character sequence has to start in column zero otherwise you get this error message:
Encountered end of line while processing a string token.
At C:\Temp\foo.ps1:1 char:3
+ @’ <<<<
The final gotcha to watch out for is that you can’t nest here strings within another here string of the same ilk (single quoted or double quoted). What this means for our commenting out script scenario is that you won’t be able to surround a chunk of script that uses a single quoted here strings with another single quoted here string to comment out that code.