Powershell Comments Examples


Like any other scripting or programming language, PowerShell provides comments to help make the scripts more readable and maintainable.

Below is a list of comments that are supported by Microsoft PowerShell with Examples.

  1. Single-Line Comment:

    Single-Line comments in PowerShell start with # hash sign.

    Example:
    # Example of Single Line Comment in PowerShell
    
    $no = 20 # This is also a Single Line Comment.
    

  2. Multi-Line Comment:

    Multi-line commands start with <# and ends with #>

    Example:
    <#
         This is an example of
         Multi-line comment in PowerShell.
         You can add as many lines as you 
         want.
    #>
    

Let's see a practical usage of both these types of comments with a real-world example.

PowerShell Single Multi Line Comment Example
<#

  PowerShell script to convert CSV to PSV
  Author: Code2care.org
  Version: 1.1
  Today's date: 2023-07-21


#>


# Input file (CSV) and output file (PSV) paths
$csvFile = "2023_data.csv"
$psvFile = "2023_data.psv"

# Import the CSV data using Import-Csv
$csvData = Import-Csv -Path $csvFilePath

# Convert each row to a PSV-formatted string
$psvData = $csvData | ForEach-Object {
    # Join the properties with the pipe (|) separator
    $psvRow = $_.Name, $_.Age, $_.Location -join "|"
    # Output the PSV-formatted string
    $psvRow
}

# Save the PSV data to the output file
$psvData | Out-File -FilePath $psvFilePath

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap