June 6th, 2015

Weekend Scripter: Find Coolest PowerShell Parameter Names

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy, Ed Wilson, talks about finding the coolest Windows PowerShell parameter names.

Microsoft Scripting Guy, Ed Wilson, is here. This morning, my friend and colleague, Dr. Thomas Shinder, posted on Facebook that his favorite Windows PowerShell parameter name is –NoClobber. Interestingly enough, when I was writing my Windows PowerShell Step by Step book, one of the editors suggested that I change –NoClobber to something else. I had to tell her that I could not change it because it was actually part of the code.

So what are the parameters that are used by Windows PowerShell cmdlets and functions? To an extent, that depends on what modules are installed. For example, I have the MSOnline module installed, in addition to the PSCX module. So, the available parameters on my system could be a bit different than those on your system. Luckily, I can use Windows PowerShell to solve the problem.

The first thing I like to do is look at one cmdlet. If I can find the information I want with one cmdlet, I can easily abstract the methodology to all cmdlets and functions. For exploration, I will use the Get-Alias cmdlet. Obviously, I will be using the Get-Command cmdlet because it knows how to look at cmdlets. Here is my first foray:

PS C:\> Get-Command Get-Alias

CommandType     Name                            ModuleName

———–                —-                                   ———-

Cmdlet               Get-Alias                          Microsoft.PowerShel…

OK. That was not too exciting. So I decide to pipe the output to the Format-List cmdlet:

Get-Command Get-Alias | Format-List *

In the following image, I can see that there is lots of cool information that Get-Command obtains about the Get-Alias cmdlet:

Image of command output

I know that I am most interested in the Parameters property. I also can see from the previous image that there are objects stored in the Parameters property, so I know that I want to expand the property. To do this, I pipe the output to the Select-Object cmdlet (Select is an alias), and I use the –ExpandProperty parameter:

Get-Command Get-Alias | select -ExpandProperty parameters

The output from the command is shown here:

Image of command output

Obviously, I need the Key property. As shown here, when I select the Key property, nothing happens:

PS C:\> Get-Command Get-Alias | select -ExpandProperty parameters | select key

key

What has happened is that they renamed the property. I find this by piping the output to the Get-Member cmdlet (gm is an alias). I see the property is actually Keys.

PS C:\> Get-Command Get-Alias | select -ExpandProperty parameters | gm

   TypeName: System.Collections.Generic.Dictionary`2[[System.String, mscorlib,

Version=4.0.0.0, Culture=neutral,

PublicKeyToken=b77a5c561934e089],[System.Management.Automation.ParameterMetadata,

System.Management.Automation, Version=3.0.0.0, Culture=neutral,

PublicKeyToken=31bf3856ad364e35]]

Name              MemberType            Definition

—-                   ———-                   ———-

Add               Method                void Add(string key, System.Management.Automat…

Clear             Method                void Clear(), void ICollection[KeyValuePair[st…

Contains          Method                bool ICollection[KeyValuePair[string,Parameter…

ContainsKey       Method                bool ContainsKey(string key), bool IDictionary…

ContainsValue     Method                bool ContainsValue(System.Management.Automatio…

CopyTo            Method                void ICollection[KeyValuePair[string,Parameter…

Equals            Method                bool Equals(System.Object obj)

GetEnumerator     Method                System.Collections.Generic.Dictionary`2+Enumer…

GetHashCode       Method                int GetHashCode()

GetObjectData     Method                void GetObjectData(System.Runtime.Serializatio…

GetType           Method                type GetType()

OnDeserialization Method                void OnDeserialization(System.Object sender), …

Remove            Method                bool Remove(string key), bool IDictionary[stri…

ToString          Method                string ToString()

TryGetValue       Method                bool TryGetValue(string key, [ref] System.Mana…

Item              ParameterizedProperty System.Management.Automation.ParameterMetadata…

Comparer          Property              System.Collections.Generic.IEqualityComparer[s…

Count             Property              int Count {get;}

IsFixedSize       Property              bool IsFixedSize {get;}

IsReadOnly        Property              bool IsReadOnly {get;}

IsSynchronized    Property              bool IsSynchronized {get;}

Keys              Property              System.Collections.Generic.Dictionary`2+KeyCol…

SyncRoot          Property              System.Object SyncRoot {get;}

Values            Property              System.Collections.Generic.Dictionary`2+ValueC…

In the following command, I choose the Keys property:

(Get-Command  | select -ExpandProperty parameters).keys

As shown here, I receive a rather random output of parameter names:

Image of command output

I want to sort the parameter names, and I also want to group them. I do not want the elements, so I come up with the following command:

(Get-Command  | select -ExpandProperty parameters).keys | sort | group –NoElement

The command and its associated output are shown here:

Image of command output

This gives me an alphabetical list of parameters. If I want to find the coolest parameter names, all I need to do is to scroll down the list. There are some pretty cool ones, but I think I like –XPATH…it is sort of like an XFiles, only different.

What cmdlet uses XPath as a parameter name? Easy. I use the Get-Command cmdlet to find it. Here is the command and output:

PS C:\> Get-Command -ParameterName xpath

CommandType     Name                           ModuleName

———–               —-                                   ———-

Cmdlet               Select-Xml                     Microsoft.PowerShel…

If I want to see which parameters are most popular, I simply add an additional sort. This command is shown here:

(Get-Command  | select -ExpandProperty parameters).keys | sort | group –NoElement | sort count –Descending

The command and its associated output are shown in the following image:

Image of command output

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.