{"id":4225,"date":"2013-02-02T00:01:00","date_gmt":"2013-02-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/02\/02\/weekend-scripter-sort-process-names-by-length-of-name\/"},"modified":"2013-02-02T00:01:00","modified_gmt":"2013-02-02T00:01:00","slug":"weekend-scripter-sort-process-names-by-length-of-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-sort-process-names-by-length-of-name\/","title":{"rendered":"Weekend Scripter: Sort Process Names by Length of Name"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary: <\/strong><span style=\"font-size: 12px\">Microsoft Scripting Guy, Ed Wilson, takes some time out to play around with the <\/span><strong style=\"font-size: 12px\">Format-Wide<\/strong><span style=\"font-size: 12px\"> cmdlet.<\/span><\/p>\n<p align=\"left\">Microsoft Scripting Guy, Ed Wilson, is here. Well, the Scripting Wife and I are hanging out on the beach this weekend. Because I have a long weekend (thanks to a way cool Scripting Manager) I feel like it is a holiday or something. Well, what do I like to do when I have a holiday? I enjoy playing around with Windows PowerShell&mdash;that&rsquo;s what. Well, I was reviewing the Formatting Windows PowerShell Output chapter for my forthcoming Windows <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-First-Steps\/dp\/0735676151\/ref=sr_1_1?ie=UTF8&amp;qid=1359483254&amp;sr=8-1&amp;keywords=first+steps+powershell\" target=\"_blank\">Windows PowerShell 3.0 First Steps<\/a> book by Microsoft Press (available now with Pre-Order) and I was reminded about using the <strong>Format-Wide<\/strong> cmdlet.<\/p>\n<h2>Format-Wide? Dude!<\/h2>\n<p>Now, I will be honest, I seldom use the <strong>Format-Wide<\/strong> cmdlet&mdash;most of the time when I could use it, I simply forget about it. It is not one of my most favorite tools, nor do I find it indispensable. Which means, there is probably some things I have not been doing with it.<\/p>\n<p>Now, if I use the <strong>Get-Process<\/strong> cmdlet and pipe the process objects to the <strong>Format-Wide<\/strong> cmdlet and choose only process names, the output appears jumbled. This command and output are shown in the figure that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7080.wes-2-2-13-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7080.wes-2-2-13-01.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>The reason for the jumbled output is the length of process names that spread through the output. Hmmmmm. I wonder if there is any way to sort the process names by length of the name? Would that produce a nicer output?<\/p>\n<h2>Well Format-Wide has no length parameter<\/h2>\n<p>If I want to organize the output from the <strong>Format-Wide<\/strong> cmdlet by length of the process name, I am going to have to sort the process names by length prior to arriving at the <strong>Format-Wide<\/strong> cmdlet in the pipeline. Ok.<\/p>\n<p>But the <strong>Get-Process<\/strong> cmdlet does not have a length parameter I can use to sort by process name length. Hmm &hellip; I guess I will need to add a length property to the process object I want to use. In that way, I can use the <strong>Sort-Object<\/strong> cmdlet before entering <strong>Format-Wide<\/strong>. Sounds like a plan, so how do I do that?<\/p>\n<h2>First use a ScriptBlock with Select-Object<\/h2>\n<p>The first thing I need to do is to create a custom object with <strong>Select-Object<\/strong>. This technique of using the <strong>Select-Object<\/strong> cmdlet to create a custom object is one of my <a href=\"http:\/\/social.technet.microsoft.com\/Search\/en-US?query=tricks&amp;beta=0&amp;rn=Hey%2c+Scripting+Guy!+Blog&amp;rq=site:blogs.technet.com\/b\/heyscriptingguy\/&amp;ac=8\" target=\"_blank\">favorite Windows PowerShell tricks<\/a>. Here I need to get the length of each process name. So, I first pipe the results from <strong>Get-Process<\/strong> to the <strong>Select-Object<\/strong> cmdlet. I can choose the <strong>name<\/strong><em> <\/em>property easily enough, but to get the length of the <strong>name<\/strong> property I need to first ensure the <strong>name<\/strong> is a string, and then call a string property (<strong>length<\/strong>). The basic syntax for ensuring the process name is a string and calling the string property <strong>length<\/strong> is shown here.<\/p>\n<p style=\"padding-left: 30px\">13:57 C:\\&gt; (gps)[0].name.tostring().length<\/p>\n<p style=\"padding-left: 30px\">6<\/p>\n<p>Sweet, it works. Now I know what I want to do, so all I have to do is to create the code to do this for each of the process names. I decide to use a custom hash table type of syntax to create a synthetic property (this hash table sort of technique works with <strong>Format-Table<\/strong>, <strong>Format-List<\/strong>, but not <strong>Format-Wide<\/strong> as well&mdash;it is used to create a custom label and a computed value for a standard property. This technique is shown here to create a custom table output.<\/p>\n<p style=\"padding-left: 30px\">14:01 C:\\&gt; gps | ft name, @{label=&#8221;length&#8221;;Expression={$_.name.tostring().Length}} -auto&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;<\/p>\n<p style=\"padding-left: 30px\">armsvc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6<\/p>\n<p style=\"padding-left: 30px\">audiodg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7<\/p>\n<p style=\"padding-left: 30px\">BtwRSupportService&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 18<\/p>\n<p style=\"padding-left: 30px\">&hellip;&lt;Output Truncated&gt;<\/p>\n<p>So, I can simply replace the <strong>Format-Table<\/strong> cmdlet with the <strong>Sort-Object<\/strong> cmdlet, and I am creating a custom object that meets my needs. Here is the code so far.<\/p>\n<p style=\"padding-left: 30px\">Get-Process |<\/p>\n<p style=\"padding-left: 30px\">Select-Object name, @{label=&#8221;length&#8221;;Expression={$_.name.tostring().Length}}<\/p>\n<h2>Put it together, sort and format<\/h2>\n<p>Now, all I need to do is sort the output by the length and pipe the results to the <strong>Format-Wide<\/strong> cmdlet. Of course, <strong>Format-Wide<\/strong> does only one property at a time, so I will lose the length in the process, but that is fine. Here is the command up with which I arrived (<strong>gps<\/strong> is an alias for <strong>Get-Process<\/strong>, <strong>select<\/strong> is an alias for <strong>Select-Object<\/strong>, <strong>sort<\/strong> is an alias for <strong>Sort-Object<\/strong>, and <strong>fw<\/strong> is the alias for <strong>Format-Wide<\/strong>).<\/p>\n<p style=\"padding-left: 30px\">gps |<\/p>\n<p style=\"padding-left: 30px\">select name, @{label=&#8221;length&#8221;;Expression={$_.name.tostring().Length}} |<\/p>\n<p style=\"padding-left: 30px\">sort length |<\/p>\n<p style=\"padding-left: 30px\">fw -Property name -Column 5<\/p>\n<p>The following figure shows the command (a one-liner) and the output associated with the command. As you can see, the output is much more aesthetically pleasing.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1323.wes-2-2-13-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1323.wes-2-2-13-02.png\" alt=\"\" border=\"0\" \/><\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, takes some time out to play around with the Format-Wide cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. Well, the Scripting Wife and I are hanging out on the beach this weekend. Because I have a long weekend (thanks to a way cool Scripting Manager) I feel like it [&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":[25,3,4,61,45],"class_list":["post-4225","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-displaying-output","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, takes some time out to play around with the Format-Wide cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. Well, the Scripting Wife and I are hanging out on the beach this weekend. Because I have a long weekend (thanks to a way cool Scripting Manager) I feel like it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4225","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=4225"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4225\/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=4225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}