Get-ParameterAlias.ps1

<#PSScriptInfo
 
.VERSION 1.1
 
.GUID 38d4ae41-0bae-4279-8438-7475159537f3
 
.AUTHOR Chris Carter
 
.COMPANYNAME
 
.COPYRIGHT 2016 Chris Carter
 
.TAGS ParameterAliases CmdletParameters
 
.LICENSEURI http://creativecommons.org/licenses/by-sa/4.0/
 
.PROJECTURI https://gallery.technet.microsoft.com/Get-Cmdlet-Parameter-a74f1736
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
.SYNOPSIS
Gets parameter aliases of commands
.DESCRIPTION
Get-ParameterAlias gets the aliases of parameters for cmdlets, scripts, functions, etc. The name of a command can be passed or piped to retrieve the aliases of all its parameters if they have any.
 
Without a command name passed, Get-ParameterAlias gets parameter aliases for all commands, excluding Application types, unless a specific type is passed to the CommandType parameter.
 
By default, common parameters are excluded from results unless the ShowCommon switch is used.
 
All command defined parameters and their aliases will be retrieved by default unless a name(s) is given to the ParameterName parameter.
.PARAMETER Name
The Name of the command to get the parameter aliases from.
.PARAMETER CommandType
The type of command to retrieve: Cmdlet, Function, Script, etc. See the help for Get-Command for futher details.
.PARAMETER ParameterName
Causes this command to only output parameters whose names match this value.
.PARAMETER ShowCommon
Causes this command to output the aliases of common parameters as well.
.INPUTS
System.String
You can pipe command names to Get-ParameterAlias.
.OUTPUTS
System.Management.Automation.PSCustomObject[]
.EXAMPLE
Get-ParameterAlias
 
This command will output parameter aliases for all commands (excluding Application types) that have them.
.EXAMPLE
Get-ParameterAlias -Name Get-Command
 
This command will get the parameter aliases for the Get-Command command.
.EXAMPLE
Get-ParameterAlias -Name Get-Command -ParameterName CommandType,ArgumentList
 
This command will get the parameter alias for Get-Command's CommandType and ArgumentLIst parameters.
.EXAMPLE
Get-ParameterAlias -CommandType Cmdlet
 
This command will get the parameter aliases for all cmdlets that have them.
.EXAMPLE
Get-ParameterAlias -Name Get* -ParameterName ComputerName
 
This command will get the parameter aliases for all commands matching the Get* wildcard expression that have a parmeter ComputerName that also has an alias.
.NOTES
The idea for this script came from reading this Hey, Scripting Guy! Blog article: http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/15/weekend-scripter-discovering-powershell-cmdlet-parameter-aliases.aspx
 
.LINK
Get-Command
#>


#Requires -Version 2.0
[CmdletBinding(HelpURI='https://gallery.technet.microsoft.com/Get-Cmdlet-Parameter-a74f1736')]

Param(
    [Parameter(Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [String[]]$Name,

    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [ValidateSet('Cmdlet','ExternalScript','Filter','Function','Script')]
    [Alias("Type","CT")]
        [System.Management.Automation.CommandTypes]$CommandType="All",
    
    [Parameter(Position=1)]
    [Alias("PN")]
        [String[]]$ParameterName,

    [Alias("IC")]
        [Switch]$ShowCommon
)

Begin {
    #Internal processing function
    Function Get-ParameterAliases ($Command) {
        #Arrays to hold common parameters and output
        $cp = "Verbose","Debug","ErrorAction","WarningAction","ErrorVariable","WarningVariable","OutVariable","OutBuffer","PipelineVariable","WhatIf","Confirm","InformationAction","InformationVariable"
        $output = @()

        #By default exlude the common parameters by comparing names to the above array unless the switch is used
        if ($ShowCommon) {
            $params = $Command.Parameters.Values
        }
        else {$params = $Command.Parameters.Values | Where-Object {$cp -notcontains $_.Name}}

        foreach ($p in $params) {
            #Continue processing if the parameter has aliases
            if ($p.Aliases) {
                #If a parameter name(s) is given to the script...I
                if ($ParameterName) {
                    foreach ($pn in $ParameterName) {
                        #If the name of the parameter equals ParameterName argument, add to the output
                        if ($p.Name -eq $pn) {
                            $output += $p
                        }
                    }
                }
                #I...Otherwise output all parameters
                else {$output += $p}
            }
        }
        #If any parameters match the criteria
        if ($output) {
            #Echo out the name of the command to the console
            Write-Host "`n`n`tCommand: $($Command.Name)`n"
            #Condense output into only the parameter's name and alias(es)
            $output | Select-Object -Property Name, Aliases
        }
    }
}

Process {
    #Only process if the Name parameter recieves a value
    if ($Name) {
        foreach ($n in $Name) {
            #Get the CommandInfo object filtered By CommandType if entered excluding Application types
            $command = Get-Command -Name $n -CommandType $CommandType | Where-Object {$_.CommandType -ne "Application"}

            #Send each command to the processing function
            foreach ($c in $command) {
                Get-ParameterAliases -Command $c
            }
        }
    }
}

End {
    #Only end if there is no value in the Name parameter
    if (!$Name) {
        #Get all of the CommandInfo objects filtered by CommandType if desired, but exclude Application types
        $command = Get-Command -CommandType $CommandType | Where-Object {$_.CommandType -ne "Application"}
        
        #Send each command to the processing function
        foreach ($c in $command) {
            Get-ParameterAliases -Command $c
        }
    }
}