Friday, August 26, 2011

PowerShell – Making Comments work for you

Thought I would share this simple idea of creating sets of variables in the same PowerShell script file. You will notice that can create comments with the # symbol below.

$site = “This Site”

$IP = “192.168.1.1”

#$site = “That Site”

#$IP = “192.168.2.1”

To switch to block comments you can use starting (<#) and ending (#>) comment symbols like in the example below.

<#

$site = “This Site”

$IP = “192.168.1.1”

#>

<#

$site = “That Site”

$IP = “192.168.2.1”

#>

Managing these code blocks become very easy to just comment out the first line in the block. So essentially you are commenting out the comment starting block. Below is that example of making multi-dataset.

#<#

$site = “This Site”

$IP = “192.168.1.1”

#>

<#

$site = “That Site”

$IP = “192.168.2.1”

#>

If you are looking for even more options for setting up comments I would strongly advise looking into the PowerShellPack. Once installed and run Import-Module PowerShellPack you can now use add-ons. The add-on switch works like advertised but will uncomment non-code lines and also remove white spaces.

http://archive.msdn.microsoft.com/PowerShellPack

No comments: