Comments and Style
Prerequisites: Script parameters
Comments
# A single-line comment
$taxRate = 0.2 # after code
<#
A block comment
spanning several lines
#>
Comment-based help
A special block comment at the top of a script or function becomes help
text that Get-Help displays — just like the built-in cmdlets:
function Get-Square {
<#
.SYNOPSIS
Returns the square of a number.
.PARAMETER Number
The number to square.
.EXAMPLE
Get-Square -Number 7
#>
param([double]$Number)
$Number * $Number
}
Get-Square -Number 7
(Get-Help Get-Square).Synopsis
49
Returns the square of a number.
Style guidelines
The community PowerShell Practice and Style guide recommends:
- Full command and parameter names in scripts — no aliases (
ls,%,?) and no positional shortcuts.Get-ChildItem -Path $folderbeatsgci $folder. Verb-Nounnames with approved verbs for your own functions, and singular nouns (Get-User, notGet-Users).- PascalCase for function and parameter names.
- Four-space indentation, and opening braces on the same line.
- Output objects, not formatted text, from functions.
PSScriptAnalyzer
PSScriptAnalyzer is the standard linter. It flags aliases, unused
variables, missing -WhatIf support, and dozens of other issues:
# illustrative
Install-Module PSScriptAnalyzer -Scope CurrentUser
Invoke-ScriptAnalyzer -Path .\script.ps1
The VS Code PowerShell extension runs it as you type.
What's next
You now know enough PowerShell to automate real tasks. Try the Beginner Projects, then continue to Level 2 — Intermediate, which starts with functions.