Kae Travis

How Do You Comment Out Code in PowerShell?

A common question asked by scripting beginners is how do you comment out code in PowerShell? Luckily it’s simple.

Commenting code is always good practise for a variety of reasons; one being if you pass your script on to a colleague (or a customer!) they will want to ensure it is readable and makes logical sense. Another good reason is if you have a memory like a sieve (like me) it’s always nice to read a comment and think “Ahhh…..that’s why i did that!”.

Commenting a Single Line of PowerShell Code

Commenting a single line of PowerShell is as easy as prefixing the comment with a hash (#) like so:

#We are going to write the word "hello" to the host
write-host "hello"

Commenting Multiple Lines of PowerShell Code

When we want to comment multiple lines of PowerShell code, we use chevrons (< and >) and a hash (#) like so:

<#
Date: 16/09/2020
We are going to write the word "hello" to the host
#>
write-host "hello"
How Do You Comment Out Code in PowerShell?
How Do You Comment Out Code in PowerShell?

Leave a Reply