Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Parameter

Posted on July 21, 2010

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

For example, here are the parameters for Get-Service.

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.

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.

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.

Function Get-Parameter {

<#
.Synopsis
    Retrieve command parameter information.
.Description
    Using Get-Command, this function will return information about parameters
    for any loaded cmdlet or function. 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 function. The parameter has an alias of Name.
.Example
    PS C:\> get-parameter get-service

    Return parameter information for get-service
.Example
PS C:\Scripts> get-parameter mkdir | select Name,type
Found 6 specific parameters for 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 for 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
    https://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)
 #>

Param(
[Parameter(Position=0,Mandatory=$True,
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,
HelpMessage="Enter a cmdlet name")]
[ValidateNotNullorEmpty()]
[Alias("name")]
[string]$command
)

Process {
    #define the set of common parameters to exclude
    $common=@("Verbose",
    "Debug",
    "ErrorAction",
    "ErrorVariable",
    "WarningAction",
    "WarningVariable",
    "OutVariable",
    "OutBuffer",
    "WhatIf",
    "Confirm")

    Try {
        $data=(Get-Command -Name $command -errorAction "Stop").parameters
    }
    Catch {
        Write-Warning "Failed to find command $command"
    }
    #keep going if parameters were found
    if ($data.count -gt 0) {
        #$data is a hash table
        $params=$data.keys | where {$common -notcontains $_}
        $count=($params | measure-object).count
        #only keep going if non-common parameters were found
        write-host "Found $count non-common parameters for $command" `
        -ForegroundColor Green

        if ($count -gt 0) {
            #get information from each parameter

            $params | foreach {
                $name=$_
                $type=$data.item($name).ParameterType
                $aliases=$data.item($name).Aliases
                $attributes=$data.item($name).Attributes
                if ($attributes[0].position -ge 0) {
                    $position=$attributes[0].position
                  }
                else {
                    $position=$null
                }
                #write a custom object to the pipeline    
                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
                }
            } #foreach
        } #if $count
     } #if $data
     else {
        Write-Host "$command has no defined parameters" -ForegroundColor Red
     }
 } #process
} #end function

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.

PS C:\> gpa new-object
Found 5 non-common parameters for 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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Get Parameter”

  1. Jason Archer says:
    July 21, 2010 at 1:54 pm

    I prefer this one myself:

    http://poshcode.org/1344

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d