All solutions below work both in functions and in scripts.
The most reliable solution that should work in any call scenario, PSv2 + :
param (
[ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
[String]$Type = 'Startup'
)
($MyInvocation.MyCommand.Parameters['Type'].Attributes |
Where-Object { $_ -is [System.Management.Automation.ValidateSetAttribute] }).ValidValues
A , PSv3 +, :
Set-StrictMode -version 1, .
Set-StrictMode , , , , PSv2 .
( Set-StrictMode : , ( ).
, module, Set-StrictMode .
, , Windows PowerShell v5.1/PowerShell Core v6.0-alpha16, script .
param (
[ValidateSet('Startup', 'Shutdown', 'LogOn', 'LogOff')]
[String]$Type = 'Startup'
)
(Get-Variable Type).Attributes.ValidValues
PSv3 + (Get-Variable Type).Attributes.ValidValues :
(Get-Variable Type).Attributes | ForEach-Object { $_.ValidValues }
.. PowerShell .Attributes .ValidValues .
.Attributes - [System.Management.Automation.ValidateSetAttribute] - .ValidValues, .
, , Set-StrictMode -version 2 , .
((Get-Variable Type).Attributes |
Where-Object { $_ -is [System.Management.Automation.ValidateSetAttribute] }).ValidValues
, ( -is ), , , .ValidValues.
[variable] $Type (Get-Variable Type).Attributes $MyInvocation.MyCommand.Parameters['Type'].Attributes.
$MyInvocation.MyCommand.Parameters , .
, ( ):