Wednesday, February 14, 2007

PowerShell multi-line comments

Well, as you may know, there is no such thing. You can only comment to the end of line. But there are two work-arounds.

The first is if you simply want to uncomment some code-lines. In this case, you can surround them with an if-statement like
if ($false) {
This can be useful when developing.

This first approach has that advantage - or disadvantage - that the disabled code must be syntactically correct.

The seconds approach, utilizes the multi-line contant construction of PowerShell. Simply surround the lines with @' and '@ -
'@ > $null

whatever can be anything except that the line must not start with '@. $null is used to write the string into the big bit void. So this approach is good for writing comments.

Do not use @" and "@. The string between will be parsed and variables will be replaced.

I know that you can simply use the uncomment function from a decent code editor. But often, notepad is all you have.

PS: The @' '@ construct is also very useful, if you want to embed other things like XML-code in you script. Here is a small example -

$xml=[xml] @'


Anonymous said...

Thanks very much for this.

Ciaran Colgan said...

good tip! cheers.