Skip to content

PowerShell Style Guide

This article provides a comprehensive PowerShell style guide focusing on readability, consistency, and best practices for writing clean and maintainable scripts. This guide is based on the recommendations from the PowerShell Practice and Style Guide.

Ensure consistent indentation, line length, and capitalization across your codebase to enhance readability.

  • Use PascalCase for all public identifiers.
  • Use lowercase for language keywords.
  • Use UPPERCASE for comment-based help keywords.
Terminal window
function Write-Host {
[CmdletBinding()]
param(
[psobject]$Object,
[switch]$NoNewline
)
}

Place opening braces at the end of a line and closing braces on a new line.

Terminal window
if ($value -gt 0) {
"Positive"
} else {
"Non-Positive"
}

Start functions with [CmdletBinding()] and use param(), begin, process, end in execution order.

Terminal window
[CmdletBinding()]
param ()
process { }
end { }

Use four spaces per indentation level.

Terminal window
foreach ($item in $list) {
Do-Something -Param $item
}

Limit lines to 115 characters. Use splatting or parentheses for clean line breaks.

Terminal window
$Params = @{
Name = "Explorer"
Verbose = $true
}
Get-Process @Params
  • Surround functions with two blank lines.
  • No trailing spaces.
  • Use single spaces around operators and parameters.
Terminal window
$Value = 5 + 3

Do not use semicolons as line terminators.

Terminal window
$Options = @{
Margin = 2
Padding = 2
}
  • Avoid return. Output objects directly.
  • Leave a space between function name and parameters.
Terminal window
function MyFunction ($param1, $param2) {
...
}
  • Use <Verb>-<Noun> naming.
  • Always use [CmdletBinding()].
  • Return objects in process {} block.
  • Specify OutputType.
Terminal window
function Get-Example {
[CmdletBinding()]
[OutputType([psobject])]
param (
[int]$Id
)
process {
New-Object -TypeName psobject -Property @{ ID = $Id }
}
}
  • Keep comments updated.
  • Write in English, using complete sentences when necessary.
  • Explain reasoning, not what the code does.
Terminal window
# Adjust margin due to UI overlap
$Margin = $Margin + 2

Use single-line # or <# ... #> for longer blocks.

Terminal window
<#
.SYNOPSIS
Example of block comment usage.
#>

Align inline comments for clarity.

Terminal window
$Options = @{
Margin = 2 # Adjust for UI
Padding = 2 # Space around text
}

Always include help in your scripts.

Terminal window
function Get-Example {
<#
.SYNOPSIS
Retrieves example data.
.EXAMPLE
Get-Example
#>
}

Indent within constructs for clarity.

Terminal window
foreach ($item in $items) {
Process-Item $item
}

Use splatting instead of backticks for line continuation.

Terminal window
$Params = @{
Class = "Win32_LogicalDisk"
Filter = "DriveType=3"
}
Get-WmiObject @Params

Avoid aliases and short forms.

Terminal window
Get-Process -Name Explorer

Prefer full paths or $PSScriptRoot.

Terminal window
Get-Content "$PSScriptRoot\README.md"

Use ${Env:UserProfile} instead of ~.

Terminal window
cd ${Env:UserProfile}

Following these guidelines ensures your PowerShell scripts are clean, consistent, and easier to maintain across teams and projects.

For more details, refer to the original PowerShell Practice and Style Guide.