Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to find the longest cmdlet name.
Microsoft Scripting Guy, Ed Wilson, is here. This is an exciting time of the year, as we get ready for the PowerShell Summit in Charlotte and for the Microsoft Ignite conference in Chicago. I have been having numerous email conversations with various individuals the past week. One of the fun exchanges took place with Windows PowerShell MVP and Honorary Scripting Guy, Sean Kearney. We are planning something cool for Ignite, and we were bouncing ideas around, for example, "I wonder which Windows PowerShell cmdlet name is the longest?" I know there are some long ones, especially when it comes to the NetAdapter or TCP cmdlets, but which is longest? And of course, how long is it really?
I played around for a couple of minutes and came up with a Windows PowerShell script that provides the answer. Let’s take a look…
First I need to get the cmdlet names (and also the CIM-wrapped functions). So I use the Get-Command cmdlet to retrieve all of the cmdlets. I then pipe the CmdletInfo object to Select-Object where I create a custom object that provides the name and the length of each name. Let's take a look at the first part of that command:
Get-Command |
Select-Object @{label = 'name'; expression = {$_.name}},
I use Select-Object to create the custom object. The first property of that object is the name. I pick that up directly from the Name property of the CmdletInfo object that streams across the pipeline. To get the length of the name, I need to use the Measure-Object cmdlet.
The name of the second property of my custom object is Length. I take the name from the CmdletInfo object and pipe it to Measure-Object and tell it to count characters. I then grab the Character property. This portion of the command is shown here:
@{label = 'length'; expression = {($_.name |
Measure-Object -Character).Characters}} |
Now I need to sort the custom object by the Length property. I tell it to sort in descending order:
Sort-Object length -Descending |
Lastly, I use the Select-Object cmdlet again to return the first item from the sorted list:
Select-Object -First 1
The complete script is shown here:
Get-Command |
Select-Object @{label = 'name'; expression = {$_.name}},
@{label = 'length'; expression = {($_.name |
Measure-Object -Character).Characters}} |
Sort-Object length -Descending |
Select-Object -First 1
As shown here, when I run the script, it tells me the name of the longest cmdlet:
Dude, 47 characters long! I am sure glad for Tab completion. There is one problem with the output, and that is that the cmdlet name is sooooooooooooooooooooooooooo long that it exceeds the screen size. If I reduce the size of the output, the cmdlet name fits. This is shown here:
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
0 comments