Windows-powershell / PowerShell-master /.github /instructions /powershell-parameter-naming.instructions.md
metadata
applyTo: '**/*.ps1, **/*.psm1'
description: Naming conventions for PowerShell parameters
PowerShell Parameter Naming Conventions
Purpose
This instruction defines the naming conventions for parameters in PowerShell scripts and modules. Consistent parameter naming improves code readability, maintainability, and usability for users of PowerShell cmdlets and functions.
Parameter Naming Rules
General Conventions
- Singular Nouns: Use singular nouns for parameter names even if the parameter is expected to handle multiple values (e.g.,
Fileinstead ofFiles). - Use PascalCase: Parameter names must use PascalCase (e.g.,
ParameterName). - Descriptive Names: Parameter names should be descriptive and convey their purpose clearly (e.g.,
FilePath,UserName). - Avoid Abbreviations: Avoid using abbreviations unless they are widely recognized (e.g.,
IDfor Identifier). - Avoid Reserved Words: Do not use PowerShell reserved words as parameter names (e.g.,
if,else,function).
Units and Precision
- Include Units in Parameter Names: When a parameter represents a value with units, include the unit in the parameter name for clarity:
TimeoutSecinstead ofTimeoutRetryIntervalSecinstead ofRetryIntervalMaxSizeBytesinstead ofMaxSize
- Use Full Words for Clarity: Spell out common terms to match PowerShell conventions:
MaximumRetryCountinstead ofMaxRetriesMinimumLengthinstead ofMinLength
Alignment with Built-in Cmdlets
- Follow Existing PowerShell Conventions: When your parameter serves a similar purpose to a built-in cmdlet parameter, use the same or similar naming:
- Match
Invoke-WebRequestparameters when making HTTP requests:TimeoutSec,MaximumRetryCount,RetryIntervalSec - Follow common parameter patterns like
Path,Force,Recurse,WhatIf,Confirm
- Match
- Consistency Within Scripts: If multiple parameters relate to the same concept, use consistent naming patterns (e.g.,
TimeoutSec,RetryIntervalSecboth useSecsuffix).
Examples
Good Parameter Names
param(
[string[]]$File, # Singular, even though it accepts arrays
[int]$TimeoutSec = 30, # Unit included
[int]$MaximumRetryCount = 2, # Full word "Maximum"
[int]$RetryIntervalSec = 2, # Consistent with TimeoutSec
[string]$Path, # Standard PowerShell convention
[switch]$Force # Common PowerShell parameter
)
Names to Avoid
param(
[string[]]$Files, # Should be singular: File
[int]$Timeout = 30, # Missing unit: TimeoutSec
[int]$MaxRetries = 2, # Should be: MaximumRetryCount
[int]$RetryInterval = 2, # Missing unit: RetryIntervalSec
[string]$FileLoc, # Avoid abbreviations: FilePath
[int]$Max # Ambiguous: MaximumWhat?
)
Exceptions
- Common Terms: Some common terms may be used in plural form if they are widely accepted in the context (e.g.,
Credentials,Permissions). - Legacy Code: Existing code that does not follow these conventions may be exempted to avoid breaking changes, but new code should adhere to these guidelines.
- Well Established Naming Patterns: If a naming pattern is well established in the PowerShell community, it may be used even if it does not strictly adhere to these guidelines.