PowerShell - Cheatsheet

By xngo on January 5, 2020

# Single line comment starts with the pound key(#)
<# For multiple line comment,
    put <# at the beginning and #> at the end of code. #>
 
# Use a space followed by a backtick to signal line continuation
    Register-PSRepository -Name "PSWindowsUpdate" `
        -SourceLocation "https://www.powershellgallery.com/packages/PSWindowsUpdate/2.1.1.2"
 
# Get help
    Get-Help <cmdletname>
 
# Display Powershell module paths
    $env:PSModulePath
 
# Define function to add values $a and $b.
    Function add($a, $b)
    {
        return $a+$b
    }
    $sum = add 3 4
 
# String concatenation.
    Write-Host "Xuan" + " " + "Ngo"
 
# Variable expansion in string quote.    
    $firstname = "Xuan"
    $lastname = "Ngo"
    Write-Host "$lastname, $firstname"
    Write-Host "$($employee.lastname), $($employee.firstname)"
 
# For loop.
    for ($i=0; $i -le 10; $i++)
    {
        $value = 1000 + $i
        Write-Host $value
    }    

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.