[CmdletBinding()] is a PowerShell attribute placed at the beginning of a function or script. It enables default parameters used to manage behavior such as verbose output and error handling. Although optional, it is a recommended baseline for reusable automation because those advanced-function features become useful as automation grows. section 2.1.1 section 2.1.7
Place the attribute before [OutputType()] and the param block. Use OutputType to state the returned value type, then define typed parameters and whether each is mandatory. Together, these declarations make required inputs clearer and let built-in error handling help prevent incorrect values from producing unforeseen results. section 2.1.1
Use CmdletBinding as part of a maintainable automation layout:
- Start functions with [CmdletBinding()] before output and parameter declarations. section 2.1.1
- Start scripts with comment-based help, CmdletBinding, OutputType, and a parameter block; place module imports after parameters when needed. section 2.1.7
- Declare functions near the start of a script so later steps can call or replace them as building blocks. section 2.1.7
Archive-path function
A Set-ArchiveFilePath function begins with [CmdletBinding()] and declares a string output type. Its mandatory ZipPath and ZipPrefix parameters, plus a date parameter, support archive naming. When the archive folder is absent, the function creates it and uses Write-Verbose to report that action. section 2.1.1 section 2.1.7
Listing 2.1 Set-ArchiveFilePath function
Function Set-ArchiveFilePath{ #1
[CmdletBinding()] #2
[OutputType([string])]
param( #3
[Parameter(Mandatory = $true)]
[string]$ZipPath,
[Parameter(Mandatory = $true)]
[string]$ZipPrefix,
[Parameter(Mandatory = $true)]
[datetime]$Date
)
if(-not (Test-Path -Path $ZipPath)){ #4
New-Item -Path $ZipPath -ItemType Directory | Out-Null
Write-Verbose "Created folder '$ZipPath'" #5
}
$timeString = $Date.ToString('yyyyMMdd') #6
$ZipName = "$($ZipPrefix)$($timeString).zip" #7
$ZipFile = Join-Path $ZipPath $ZipName #8
if(Test-Path -Path $ZipFile){ #9
throw "The file '$ZipFile' already exists"
}
$ZipFile #10
}
Inspect the complete function layout: CmdletBinding precedes OutputType and param, while Write-Verbose reports folder creation during archive-path setup. section 2.1.1

