How to Comment Out Code in PowerShell

  1. PowerShell One-Line Comments Using the Comment Symbol (#)
  2. PowerShell Commenting Out Multiple Lines Using Comment Blocks
  3. PowerShell Edge Case Scenario by Using the Exit Command
How to Comment Out Code in PowerShell

If you have used other languages such as Bash, Python, and Ruby, commenting out in Windows PowerShell will be similar.

This article will discuss all use cases that we can use to comment out code in Windows PowerShell.

PowerShell One-Line Comments Using the Comment Symbol (#)

Ever since the start, PowerShell V1.0 has been shipped and released to the public with the capability of commenting code. We use the symbol (#) to comment out the code. We call this symbol by many names like the number sign or the hash, but Microsoft officially called this the comment symbol.

A single comment symbol (#) will comment out the code from the first # up until the end of the line. Of course, you could also put multiple comment symbols in one line.

Example Code:

#######################################################
# Examples of one-line comments in Windows PowerShell #
#######################################################

Get-Process -Name *host* #### You could put more.

Get-Process -Name *host* # | Stop-Service # You can use it to comment out a part of a line.

# Get-Process -Name *host* # This will comment out the whole line.

When commenting on code, it is best practice to leave a space between the comment symbol and your code. Some cmdlets use the comment symbol but not for commenting code. For example, the #REQUIRES cmdlet is a well-known PowerShell statement that will prevent a script from running unless the modules or prerequisite snap-ins are met.

Example Code:

Get-Module AzureRM.Netcore | Remove-Module
#REQUIRES -Modules AzureRM.Netcore

Using these best practices, we can avoid unnecessary errors in our script.

PowerShell Commenting Out Multiple Lines Using Comment Blocks

To comment out multiple lines of code without using multiple comment symbols per line, we can conveniently enclose our comment symbol with less than (<) and greater than (>) signs. We call this a comment block.

The comment symbol with the less than sign (<#) will act as an opening tag for our comment block, while the comment symbol with the greater than sign will serve as a closing tag (#>).

Example Code:

<#
Get-Process -Name *host*
Stop-Service -DisplayName Windows*Update -WhatIf
#>

It is worth noting that you could insert a single comment symbol in between the comment block.

Example Code:

<#
Get-Process -Name 'host1'

#Tested up until this point

Stop-Service -DisplayName Windows*Update -WhatIf
#>

However, nesting comment blocks by inserting a new set of comment block tags will result in an error.

Example Code:

<#
Nope, these are not allowed in PowerShell.

<# This will break your first multiline comment block... #>

...and this will throw a syntax error. #This line will execute the throw cmdlet
#>

Alternatively, pressing Ctrl+J and clicking Comment Block will generate a code block in your script.

PowerShell Edge Case Scenario by Using the Exit Command

Remember that the Exit command will terminate and close your scripting environment. So, anything written after the Exit command is not executed. We call this the edge case scenario. Writing something after the Exit command is possible but not recommended because other scripting environments may misread these extra lines and cause an error.

Example Code:

Get-Process -Name 'host1'
exit

Anything beyond the `<# exit #>` line is not executed inside the PowerShell scripting environment. However, as mentioned before, this is not recommended despite being possible.
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn