As I continue to work on the 2nd edition of Managing Active Directory with Windows PowerShell: TFM, I find situations where I need a tool to get information out of Windows PowerShell. For example, the cmdlets in the Microsoft Active Directory module have a lot of parameters and I wanted to know some specifics. Now I could simply keep reading the full cmdlet help, but that gets kind of tedious. Instead I remembered I can use the Get-Command cmdlet to dig into the details.
For example, here are the parameters for Get-Service.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
PS C:\> (get-command get-service).parameters Key Value --- ----- Name System.Management.Automation.ParameterMetadata ComputerName System.Management.Automation.ParameterMetadata DependentServices System.Management.Automation.ParameterMetadata RequiredServices System.Management.Automation.ParameterMetadata DisplayName System.Management.Automation.ParameterMetadata Include System.Management.Automation.ParameterMetadata Exclude System.Management.Automation.ParameterMetadata InputObject System.Management.Automation.ParameterMetadata Verbose System.Management.Automation.ParameterMetadata Debug System.Management.Automation.ParameterMetadata ErrorAction System.Management.Automation.ParameterMetadata WarningAction System.Management.Automation.ParameterMetadata ErrorVariable System.Management.Automation.ParameterMetadata WarningVariable System.Management.Automation.ParameterMetadata OutVariable System.Management.Automation.ParameterMetadata OutBuffer System.Management.Automation.ParameterMetadata |
The value, System.Management.Automation.ParameterMetadata is intriguing. The output is a dictionary object so I can check just a single parameter.
|
1 2 3 4 5 6 7 8 9 |
PS C:\> (get-command get-service).parameters.name Name : Name ParameterType : System.String[] ParameterSets : {[Default, System.Management.Automation.ParameterSetMetadata]} IsDynamic : False Aliases : {ServiceName} Attributes : {Default, System.Management.Automation.AliasAttribute} SwitchParameter : False |
I can even take it a step further and get attribute information.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
PS C:\> (get-command get-service).parameters.name.attributes Position : 0 ParameterSetName : Default Mandatory : False ValueFromPipeline : True ValueFromPipelineByPropertyName : True ValueFromRemainingArguments : False HelpMessage : HelpMessageBaseName : HelpMessageResourceId : TypeId : System.Management.Automation.ParameterAttribute AliasNames : {ServiceName} TypeId : System.Management.Automation.AliasAttribute |
Now that I know where to go and how to get what I need, I can write a function to retrieve parameters for a given cmdlet or function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
Function Get-Parameter { <<span class="rem">#</span> .Synopsis Retrieve command parameter information. .Description Using Get-Command, this <span class="kwrd">function</span> will <span class="kwrd">return</span> information about parameters <span class="kwrd">for</span> any loaded cmdlet or <span class="kwrd">function</span>. The common parameters like Verbose and ErrorAction are omitted. Get-Parameter returns a custom object with the most useful information an administrator might need to know. Here is an example: Position : 0 Name : Name Type : System.String[] Aliases : {ServiceName} ValueFromPipeline : True Mandatory : False ParameterSet : Default .Parameter Command The name of a cmdlet or <span class="kwrd">function</span>. The parameter has an alias of Name. .Example PS C:\> get-parameter get-service Return parameter information <span class="kwrd">for</span> get-service .Example PS C:\Scripts> get-parameter mkdir | select Name,type Found 6 specific parameters <span class="kwrd">for</span> mkdir Name Type ---- ---- Path System.String[] Name System.String Value System.Object Force System.Management.Automation.SwitchParameter Credential System.Management.Automation.PSCredential UseTransaction System.Management.Automation.SwitchParameter .Example PS C:\Scripts> get-parameter get-wmiobject | sort parameterset | format-table -GroupBy ParameterSet -Property Name,Alias,Position,Type Found 18 non-common parameters <span class="kwrd">for</span> get-wmiobject ParameterSet: __AllParameterSets Name Alias Position Type ---- ----- -------- ---- ThrottleLimit System.Int32 Amended System.Management.Automation.SwitchParameter AsJob System.Management.Automation.SwitchParameter ParameterSet: class Name Alias Position Type ---- ----- -------- ---- Locale System.String Impersonation System.Management.ImpersonationLevel ParameterSet: list Name Alias Position Type ---- ----- -------- ---- Namespace System.String ComputerName System.String[] Authority System.String List System.Management.Automation.SwitchParameter Recurse System.Management.Automation.SwitchParameter ParameterSet: query Name Alias Position Type ---- ----- -------- ---- Filter System.String Property 1 System.String[] Class 0 System.String EnableAllPrivileges System.Management.Automation.SwitchParameter DirectRead System.Management.Automation.SwitchParameter ParameterSet: WQLQuery Name Alias Position Type ---- ----- -------- ---- Query System.String Credential System.Management.Automation.PSCredential Authentication System.Management.AuthenticationLevel .Inputs [string] .Outputs custom object .Link http://jdhitsolutions.com/blog/2010/07/get-parameter .Link Get-Command .Notes NAME: Get-Parameter VERSION: 1.2 AUTHOR: Jeffery Hicks LASTEDIT: July 20, 2010 Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) <span class="rem">#></span> Param( [Parameter(Position=0,Mandatory=$True, ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True, HelpMessage=<span class="str">"Enter a cmdlet name"</span>)] [ValidateNotNullorEmpty()] [Alias(<span class="str">"name"</span>)] [string]$command ) Process { <span class="rem">#define the set of common parameters to exclude</span> $common=@(<span class="str">"Verbose"</span>, <span class="str">"Debug"</span>, <span class="str">"ErrorAction"</span>, <span class="str">"ErrorVariable"</span>, <span class="str">"WarningAction"</span>, <span class="str">"WarningVariable"</span>, <span class="str">"OutVariable"</span>, <span class="str">"OutBuffer"</span>, <span class="str">"WhatIf"</span>, <span class="str">"Confirm"</span>) Try { $data=(Get-Command -Name $command -errorAction <span class="str">"Stop"</span>).parameters } Catch { Write-Warning <span class="str">"Failed to find command $command"</span> } <span class="rem">#keep going if parameters were found</span> <span class="kwrd">if</span> ($data.count <span class="preproc">-gt</span> 0) { <span class="rem">#$data is a hash table</span> $params=$data.keys | where {$common -notcontains $_} $count=($params | measure-object).count <span class="rem">#only keep going if non-common parameters were found</span> write-host <span class="str">"Found $count non-common parameters for $command"</span> ` -ForegroundColor Green <span class="kwrd">if</span> ($count <span class="preproc">-gt</span> 0) { <span class="rem">#get information from each parameter</span> $params | <span class="kwrd">foreach</span> { $name=$_ $type=$data.item($name).ParameterType $aliases=$data.item($name).Aliases $attributes=$data.item($name).Attributes <span class="kwrd">if</span> ($attributes[0].position <span class="preproc">-ge</span> 0) { $position=$attributes[0].position } <span class="kwrd">else</span> { $position=$null } <span class="rem">#write a custom object to the pipeline </span> New-Object -TypeName PSObject -Property @{ Name=$name Aliases=$aliases Mandatory=$attributes[0].mandatory Position=$position ValueFromPipeline=$attributes[0].ValueFromPipeline Type=$type ParameterSet=$attributes[0].ParameterSetName } } <span class="rem">#foreach</span> } <span class="rem">#if $count</span> } <span class="rem">#if $data</span> <span class="kwrd">else</span> { Write-Host <span class="str">"$command has no defined parameters"</span> -ForegroundColor Red } } <span class="rem">#process</span> } <span class="rem">#end function</span> |
The function has comment based help and examples so be sure to take a look. Otherwise the function is pretty straight forward. I use it with an alias, gpa.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
PS C:\> gpa new-object Found 5 non-common parameters <span class="kwrd">for</span> new-object Position : 0 Name : TypeName Type : System.String Aliases : {} ValueFromPipeline : False Mandatory : True ParameterSet : Net Position : 0 Name : ComObject Type : System.String Aliases : {} ValueFromPipeline : False Mandatory : True ParameterSet : Com Position : 1 Name : ArgumentList Type : System.Object[] Aliases : {Args} ValueFromPipeline : False Mandatory : False ParameterSet : Net Position : Name : Strict Type : System.Management.Automation.SwitchParameter Aliases : {} ValueFromPipeline : False Mandatory : False ParameterSet : Com Position : Name : Property Type : System.Collections.Hashtable Aliases : {} ValueFromPipeline : Mandatory : ParameterSet : |
I’m sure it needs a few tweaks so let me know what works and what doesn’t. You can download Get-Parameter.ps1 here.

I prefer this one myself:
http://poshcode.org/1344