Effective PowerShell Item 4: Commenting Out Lines in a Script File

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:\> @"
>> $(get-process)
>> "@
System.Diagnostics.Process (audiodg) System.Diagnostics.Process (csrss) …

However a single quoted here string doesn’t do this:

PS C:\> @’
>> $(get-process)
>> ‘@

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. 

This entry was posted in Effective PowerShell. Bookmark the permalink.

One Response to Effective PowerShell Item 4: Commenting Out Lines in a Script File

  1. John says:

    Another gotcha about here-strings is that even though they look like C# verbatim strings, they\’re quite different. Here\’s a little table listing six differences between PowerShell here-strings and C# verbatim strings: http://odin.mdacc.tmc.edu/~cook/PowerShellCookbook.html#22. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s