{"id":7131,"date":"2015-03-22T00:01:00","date_gmt":"2015-03-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/22\/weekend-scripter-use-powershell-to-find-longest-cmdlet-name\/"},"modified":"2019-02-18T10:30:11","modified_gmt":"2019-02-18T17:30:11","slug":"weekend-scripter-use-powershell-to-find-longest-cmdlet-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-find-longest-cmdlet-name\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Find Longest Cmdlet Name"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to find the longest cmdlet name.<\/span><\/p>\n<p>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, &quot;I wonder which Windows PowerShell cmdlet name is the longest?&quot; I know there are some long ones, especially when it comes to the <b>NetAdapter<\/b> or <b>TCP<\/b> cmdlets, but which is longest? And of course, how long is it really?<\/p>\n<p>I played around for a couple of minutes and came up with a Windows PowerShell script that provides the answer. Let&rsquo;s take a look&hellip;<\/p>\n<p>First I need to get the cmdlet names (and also the CIM-wrapped functions). So I use the <b>Get-Command<\/b> cmdlet to retrieve all of the cmdlets. I then pipe the <b>CmdletInfo<\/b> object to <b>Select-Object<\/b> where I create a custom object that provides the name and the length of each name. Let&#039;s take a look at the first part of that command:<\/p>\n<p style=\"margin-left:30px\">Get-Command |<\/p>\n<p style=\"margin-left:30px\">Select-Object @{label = &#039;name&#039;; expression = {$_.name}},&nbsp;<\/p>\n<p>I use <b>Select-Object<\/b> to create the custom object. The first property of that object is the name. I pick that up directly from the <b>Name<\/b> property of the <b>CmdletInfo<\/b> object that streams across the pipeline. To get the length of the name, I need to use the <b>Measure-Object<\/b> cmdlet.<\/p>\n<p>The name of the second property of my custom object is <b>Length<\/b>. I take the name from the <b>CmdletInfo<\/b> object and pipe it to <b>Measure-Object<\/b> and tell it to count characters. I then grab the <b>Character<\/b> property. This portion of the command is shown here:<\/p>\n<p style=\"margin-left:30px\">@{label = &#039;length&#039;; expression = {($_.name |<\/p>\n<p style=\"margin-left:30px\">&nbsp; Measure-Object -Character).Characters}} |<\/p>\n<p>Now I need to sort the custom object by the <b>Length<\/b> property. I tell it to sort in descending order:<\/p>\n<p style=\"margin-left:30px\">Sort-Object length -Descending |&nbsp;<\/p>\n<p>Lastly, I use the <b>Select-Object<\/b> cmdlet again to return the first item from the sorted list:<\/p>\n<p style=\"margin-left:30px\">Select-Object -First 1<\/p>\n<p>The complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Command |<\/p>\n<p style=\"margin-left:30px\">Select-Object @{label = &#039;name&#039;; expression = {$_.name}},<\/p>\n<p style=\"margin-left:30px\">@{label = &#039;length&#039;; expression = {($_.name |<\/p>\n<p style=\"margin-left:30px\">&nbsp; Measure-Object -Character).Characters}} |<\/p>\n<p style=\"margin-left:30px\">Sort-Object length -Descending |<\/p>\n<p style=\"margin-left:30px\">Select-Object -First 1<\/p>\n<p>As shown here, when I run the script, it tells me the name of the longest cmdlet:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-22-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-22-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>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:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-22-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-22-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,61,45],"class_list":["post-7131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=7131"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7131\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=7131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}