PowerShell Naming Conventions: Functions, Parameters and Variables

PowerShell naming conventions come down to one pattern for commands: an approved verb, a hyphen, and a singular noun, both in PascalCase. Parameters use PascalCase too. Here’s a function that follows all of it:

function Get-DiskReport {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string[]]$ComputerName,

        [int]$MinimumFreeGB = 10
    )
    foreach ($computer in $ComputerName) {
        "Checking $computer for less than $MinimumFreeGB GB free"
    }
}

Get-DiskReport -ComputerName WEB01, SQL01

Output:

Checking WEB01 for less than 10 GB free
Checking SQL01 for less than 10 GB free

Anyone who knows Get-Service or Get-ChildItem can guess what Get-DiskReport does and how to call it. That’s the whole point of the conventions.

I checked each rule below in PowerShell 7.6 and Windows PowerShell 5.1, and I show where the two versions differ.

Use an approved verb

PowerShell keeps a list of approved verbs, and Get-Verb shows it, along with the group each verb belongs to:

Get-Verb -Verb Get, New, Remove, Invoke | Select-Object -Property Verb, Group

Output:

Verb   Group
----   -----
Get    Common
New    Common
Remove Common
Invoke Lifecycle

To check a verb before you use it, pass it to Get-Verb. An empty result means it isn’t approved:

foreach ($verb in 'Get', 'New', 'Fetch', 'Create', 'Delete', 'Build') {
    '{0,-7} approved: {1}' -f $verb, [bool](Get-Verb -Verb $verb)
}

Output in PowerShell 7:

Get     approved: True
New     approved: True
Fetch   approved: False
Create  approved: False
Delete  approved: False
Build   approved: True
PowerShell Get-Verb checking whether verbs are approved in Windows PowerShell 5.1
Build isn’t an approved verb in Windows PowerShell 5.1

Build is approved in PowerShell 7 but not in 5.1. Microsoft added Build and Deploy in PowerShell 6, which is why the counts differ:

"Approved verbs: $((Get-Verb).Count)"

Output in PowerShell 7:

Approved verbs: 100

Windows PowerShell 5.1 reported 98. If your module must work in 5.1, pick verbs that exist in both.

Verbs to avoid and what to use instead

Microsoft asks you not to use synonyms of approved verbs. These are the swaps I see most often in scripts:

Instead ofUseWhy
Create, Make, GenerateNewNew creates a resource
Delete, Erase, DiscardRemoveRemove deletes a resource
Obtain, Acquire, DumpGetGet retrieves a resource
RunInvokeInvoke performs an action and waits
Kill, Terminate, EndStopStop ends an activity
Verify, Diagnose, AnalyzeTestTest verifies a resource

Microsoft’s list of approved verbs describes each verb and gives the full list of synonyms to avoid.

Approved verbs matter most when you name your own commands, so see functions in PowerShell next.

What happens if you use an unapproved verb?

Nothing breaks, but anyone who imports your module gets a warning. This test module has a Fetch-Report function:

Import-Module -Name C:\psfaqs\DemoTools -Force
'--- same module with -DisableNameChecking:'
Import-Module -Name C:\psfaqs\DemoTools -Force -DisableNameChecking

Output in PowerShell 7:

WARNING: The names of some imported commands from the module 'DemoTools' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.
--- same module with -DisableNameChecking: