Basic Syntax
- Naming convention: Verb-Noun (Get-Process, Set-Content), unified and discoverable
- Parameter syntax: -ParameterName Value, supports short names and positional parameters; supports splatting (@params)
- Case sensitivity: Case-insensitive by default
- Comments:
- Line comment:
# ... - Block comment:
<# ... #>
- Line comment:
- Variables: Start with $ ($name), types are automatically inferred as needed
- Statements and blocks: {} represents script blocks, if/elseif/else, switch, for/foreach/while/do{}while, etc.
- Module loading: Import-Module; automatic module loading is handled by module manifest and command discovery mechanism
- Error types: Terminating and Non-terminating; $ErrorActionPreference controls behavior
Example: Parameter splatting with pipeline
powershell
$params = @{ Path='C:\temp\log.txt'; Value='hello'; Encoding='utf8' }
Set-Content @params
Get-Content $params.Path | Measure-Object -Line -CharacterScript structure recommendations:
- Top comments describe purpose, parameters, examples
- Use [CmdletBinding()] and Param() to define advanced function parameters and pipeline binding
- Reasonable use of Write-Verbose/Write-Debug/Write-Information for controlled logging