PO

PowerShell Defaults

Core PowerShell coding conventions covering naming, cmdlet design, and script structure

Details

Language / Topic
powershellPowerShell
Category
Style Guide

Rules

balanced
- Use approved Verb-Noun cmdlet names (e.g., `Get-Item`, `Set-Content`, `Invoke-RestMethod`) — run `Get-Verb` to see the full list of approved verbs.
- Use `PascalCase` for function names, parameter names, and variable names — PowerShell is case-insensitive but `$MyVariable` is clearer than `$myvariable`.
- Add `[CmdletBinding()]` to every advanced function to enable `-Verbose`, `-Debug`, `-WhatIf`, and `-ErrorAction` common parameters automatically.
- Use `param()` blocks with explicit `[Parameter()]` attributes and type constraints instead of positional argument parsing.
- Validate parameters with `[ValidateNotNullOrEmpty()]`, `[ValidateSet('a','b')]`, or `[ValidateRange(1,100)]` attributes rather than manual `if` checks inside the function body.
- Use `Write-Verbose` for diagnostic output and `Write-Output` (or just emit objects) for pipeline data — never use `Write-Host` in library functions as it bypasses the pipeline.
- Prefer objects over formatted strings as function output — return `[PSCustomObject]@{Name=$name; Value=$val}` so callers can pipe, filter, and format as needed.
- Use `#Requires -Version 5.1` or `#Requires -Modules Az` at the top of scripts to fail fast with a clear message on incompatible environments.