Dynamic parameters

PowerShell has always been an extensible language meaning that you can add things on, change things and even remove things if required. One way that this extensibility surfaces is dynamic parameters.

A dynamic parameter is a parameter that is available when certain conditions are met. Many of the core cmdlets (about_Core_Commands) have dynamic parameters that are available dependent on the PowerShell provider currently in use.

A PowerShell provider exposes a data store in the same way as the file system

PS> Get-PSProvider | select Name, Drives

Name        Drives
—-        ——
Registry    {HKLM, HKCU}
Alias       {Alias}
Environment {Env}
FileSystem  {C, D}
Function    {Function}
Variable    {Variable}
Certificate {Cert}

You can view the available certificates.

Get-ChildItem -Path Cert:\CurrentUser\ –Recurse

Now view the syntax of Get-ChildItem

PS> Get-Command  Get-ChildItem -Syntax

Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-Depth <uint32>] [-Force] [-Name] [-UseTransaction] [-Attributes <FlagsExpression[FileAttributes]>] [-Directory] [-File] [-Hidde
n] [-ReadOnly] [-System] [<CommonParameters>]

Get-ChildItem [[-Filter] <string>] -LiteralPath <string[]> [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-Depth <uint32>] [-Force] [-Name] [-UseTransaction] [-Attributes <FlagsExpression[FileAttributes]>] [-Directory] [-File] [-Hi
dden] [-ReadOnly] [-System] [<CommonParameters>]

Move into the cert: drive and have another look at Get-ChildItem

PS> Get-Command  Get-ChildItem -Syntax

Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-Depth <uint32>] [-Force] [-Name] [-UseTransaction] [-CodeSigningCert] [-DocumentEncryptionCert] [-SSLServerAuthentication] [-DnsName <DnsNameRepresentation>] [-Eku <string[]>] [-ExpiringInDays <int>] [<CommonParameters>]

Get-ChildItem [[-Filter] <string>] -LiteralPath <string[]> [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-Depth <uint32>] [-Force] [-Name] [-UseTransaction] [-CodeSigningCert] [-DocumentEncryptionCert] [-SSLServerAuthentication] [
-DnsName <DnsNameRepresentation>] [-Eku <string[]>] [-ExpiringInDays <int>] [<CommonParameters>]

Comparing the 2 syntax lists you’ll see a number of differences. These are the dynamic parameters.

You can use –Eku for instance for filter the certificates

Get-ChildItem -Path Cert:\CurrentUser\ -Recurse -Eku ‘Client’

If you try to run that command from any drive but cert: it’ll fail.

You can discover the dynamic parameters that are available for a particular provider by looking at the provider’s help

Get-Help certificate

When you’re working with PowerShell providers and drives remember to check what dynamic parameters are available – could save you some time and effort.

Note these dynamic parameters don’t appear to be available in PowerShell v6

Advertisements
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s