PowerShell Syntax Cheat Sheet
A simple guide to understand
PowerShell commands
Visual Structure of a PowerShell Command
• Command-Name [-ParameterName <Argument>] [-Switch]
[<CommonParameters>]
• Example:
• Get-ChildItem -Path "C:\" -Recurse -Include "*.txt" -Verbose
• | Action | Parameter | Argument | Switch | Argument | Common |
• |--------------|-----------|------------|--------|-----------|--------|
PowerShell vs Real-Life Analogies
• | PowerShell Concept | Real-Life Example | Example
|
• |-----------------------|---------------------------------|-----------------------------------|
• | Command Name | A verb + noun ('Open Door') | Get-ChildItem
|
• | Parameter w/ Argument | 'Open the red door' | -Path "C:\"
|
• | Switch Parameter | 'Turn on the light' (toggle) | -Recurse
|
• | Positional Parameter | 'Find [dog]' | Get-ChildItem "C:\"
|
• | Multi-Value Argument | 'Search for dogs and cats' | -Include "*.txt",
"*.log" |
Symbols Cheat Sheet
• | Symbol | Meaning | Example
|
• |---------|-----------------------------------|--------------------|
• | [ ] | Optional parameter | [-Path] |
• | < > | Required argument (type expected) |
<string> or <int> |
• | [] after < > | Accepts multiple values (array)|
<string[]> |
• |- | Prefix for a parameter | -Recurse, -
Path |
Common Cmdlets for Practice
• | Cmdlet | Description | Example |
• |-------------------|--------------------------------|-----------------------------|
• | Get-ChildItem | Lists files and folders | Get-ChildItem -Path
"C:\" |
• | Get-Service | Shows running services | Get-Service -
Name "wuauserv" |
• | Set-Location | Changes the current directory | Set-Location -
Path "C:\" |
• | Get-Help | Displays help for any command | Get-Help Get-
ChildItem |
• | Start-Process | Starts a process | Start-Process
notepad.exe |
Common Errors and Fixes
• | Error | Cause |
Solution |
• |---------------------------------------|------------------------------------------
-|---------------------------------------|
• | Missing argument for parameter | Argument for
mandatory parameter missing | Add the required argument.
|
• | Cannot find path 'C:\InvalidPath' | The file/folder path does
not exist | Double-check the path. |
• | Unexpected token | Syntax error in the command
| Check for missing/extra brackets or quotes |
Quick Practice Scenarios
• 1. **List all files in a folder:**
• Get-ChildItem -Path "C:\Users" -Recurse
• 2. **Find a specific service by name:**
• Get-Service -Name "wuauserv"
• 3. **Show help for a cmdlet:**
• Get-Help Get-ChildItem
Top Tips for Beginners
• - Start with **`Get-Help`** to explore any cmdlet.
• Example: Get-Help Get-ChildItem -Full
• - Use **tab completion** to avoid typing errors.
• - Test commands with `-WhatIf` to avoid
unintended changes.
• Example: Remove-Item -Path "C:\Temp" -Recurse -
WhatIf