- Rule: Keywords in comment-based help must be uppercase
- Examples:
.SYNOPSIS .DESCRIPTION .PARAMETER .EXAMPLE .INPUTS .OUTPUTS .NOTES
- Rule: Use PascalCase for all public identifiers
- Examples:
function Get-Process {} $Global:ConfigurationValue $Script:LoggingPreference
- Rule: Use Verb-Noun format with PascalCase for both parts
- Examples:
function Get-UserProfile {} function Start-ProcessManager {} function Update-ServiceConfiguration {}
- Rule: Capitalize both letters in two-letter acronyms
- Examples:
$PSDefaultParameterValues Get-PSHost $VMId
- Rules:
- Opening brace: Place at end of line
- Closing brace: Place at beginning of line
- Exception: Small scriptblocks passed as parameters
- Examples:
if ($value) { # code } Get-Process | Where-Object { $_.CPU -gt 10 }
- Rule: All functions must include CmdletBinding and standard blocks
- Example:
function Verb-Noun { [CmdletBinding()] param () begin { } process { } end { } }
- Rule: Maximum 115 characters per line
- Example:
Get-ChildItem -Path $LongPath | Where-Object { $_.Length -gt 100MB } | Select-Object Name, Length
- Rules:
- Single space around parameter names and operators
- Exceptions: Switch parameters and unary operators
- Examples:
$result = Get-Content -Path $file -Wait:$true $count++ $date = (Get-Date).AddDays(-1)
- Rules:
- No trailing whitespace
- Single space inside braces/parentheses
- No spaces inside parentheses/brackets
- Examples:
$( Get-Process ).Count ${variable} function Get-Example { $value }
- Rules:
- Don't use return keyword
- Output objects in process block
- Example:
process { $result }
- Rule: Specify OutputType for advanced functions
- Examples:
[OutputType([System.String])] [OutputType('System.IO.FileInfo', ParameterSetName = 'Path')]
- Rule: Always provide DefaultParameterSetName when using parameter sets
- Example:
[CmdletBinding(DefaultParameterSetName = 'Path')] param ( [Parameter(ParameterSetName = 'Path')] [string]$Path, [Parameter(ParameterSetName = 'LiteralPath')] [string]$LiteralPath )
- Rule: Use parameter validation attributes instead of body validation
- Examples:
# AllowNull param ( [AllowNull()] [string]$ComputerName ) # ValidateRange param ( [ValidateRange(0, 100)] [int]$Percentage ) # ValidateSet param ( [ValidateSet('Low', 'Medium', 'High')] [string]$Priority )
- Rule: Place comment-based help inside function at the beginning
- Example:
function Get-Example { <# .SYNOPSIS Brief description .DESCRIPTION Detailed description .PARAMETER Name Parameter description .EXAMPLE Example usage #> [CmdletBinding()] param () process { } }
- Rule: All script files should end with a single blank line
- Example:
function Get-Process { # Function code here } # This is the last line of code # Below this comment is a single blank line