There is nothing more frustrating than looking at Powershell code from someone else – who didn’t bother to comment the code. 1000 lines without a clue of what is happening, unless you understand the relevance of each command in context. Uhg.
In Powershell one can use the # symbol (Pound or Hashtag) to denote information that is “notes” and not part of the code.
For example I will denote section headers with large blocks of # signs for visibility:
############################################
#
# This section is about commenting your code
#
############################################
and smaller notes with a smaller comment:
####################
## Loop through this
You can also add a # at the end to comment out a piece – for example if I use a whatif for testing but don’t want it there once the code is “production”.
$user = Get-Aduser -name $inputname #-whatif
NOTE: Whereever the #starts, the code ends.
In additon you can use a “Regions” to denote sections of your code. Like college football conferences… but with less gerrymandering.
To do this we use the # sign and the word “region” followed by a description to start the section:
#region Our Variables
And then # and “endregion” to end the section.
#endregion
Most powershell editors, like PowerGui Script editor, will then allow you to collapse the region – makes browsing over sections of code:
I hope you find this useful.