{"id":1525,"date":"2009-06-02T05:15:21","date_gmt":"2009-06-02T13:15:21","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2009\/06\/02\/june-09-powershell-one-liner\/"},"modified":"2009-06-02T05:15:21","modified_gmt":"2009-06-02T13:15:21","slug":"june-09-powershell-one-liner","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/06\/02\/june-09-powershell-one-liner\/","title":{"rendered":"June &#8217;09 PowerShell One-Liner"},"content":{"rendered":"<p>In case you don&#8217;t get the SAPIEN Newsletter, here&#8217;s my PowerShell one-liner for the month.<\/p>\n<p>The <strong>Get-Command<\/strong> cmdlet can return information not only about cmdlets but other applications and scripts that PowerShell &#8220;knows&#8221; about. This month I have a PowerShell one-liner that uses <strong>Get-Command<\/strong> to find all PowerShell scripts in any folder in your path. Using a series of <strong>Select-Object<\/strong> expressions, the one-liner displays the script name, location, its size, when it was created, when it was last modified and the status of its digital signature (using <strong>Get-AuthenticodeSiganture<\/strong>. You do sign your production scripts don&#8217;t you? I&#8217;m sorting the output by folder and size but you may have other uses for this information. This is technically a one-line expression but I&#8217;ve split it up for legibility.<\/p>\n<div style=\"border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px\">\n<div style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">Get-Command -commandtype externalscript | <\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">select Name,@{name=<span style=\"color: #006080\">\"Folder\"<\/span>;expression={<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">Split-Path $_.definition -parent}},@{<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">name=<span style=\"color: #006080\">\"Size\"<\/span>;Expression={<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">(Get-ChildItem $_.definition).length}},@{<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">name=<span style=\"color: #006080\">\"Created\"<\/span>;Expression={<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">(get-childitem $_.definition).CreationTime}},@{<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">name=<span style=\"color: #006080\">\"Modified\"<\/span>;Expression={<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">(get-childitem $_.definition).LastWriteTime}},@{<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">name=<span style=\"color: #006080\">\"Signature\"<\/span>;Expression={<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">(Get-AuthenticodeSignature $_.definition).Status }} |<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">Sort Folder,Size<\/pre>\n<\/div>\n<\/div>\n<p>Here&#8217;s a sample of what you can expect:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Initialize-VIToolkitEnvironment.ps1<br \/>Folder&nbsp;&nbsp;&nbsp; : C:\\Program Files (x86)\\VMware\\Infrastructure\\VIToolkitForWindows\\Scripts<br \/>Size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 7749<br \/>Created&nbsp;&nbsp; : 1\/20\/2009 9:01:34 PM<br \/>Modified&nbsp; : 1\/20\/2009 9:01:34 PM<br \/>Signature : Valid <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : voicedemo.ps1<br \/>Folder&nbsp;&nbsp;&nbsp; : C:\\Windows<br \/>Size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 82<br \/>Created&nbsp;&nbsp; : 12\/23\/2008 3:13:51 PM<br \/>Modified&nbsp; : 12\/4\/2006 1:53:32 PM<br \/>Signature : NotSigned<\/font><\/p>\n<p>To take this further I would turn this into a function or scriptblock to make it easy to reuse.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px\" id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:1623af19-8cd1-45f5-b922-00fc2cc28480\" class=\"wlWriterSmartContent\">\n<p>Download a copy of this oneliner <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/06\/june09-oneliner.txt\" target=\"_blank\">here.<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In case you don&#8217;t get the SAPIEN Newsletter, here&#8217;s my PowerShell one-liner for the month that uses Get-Command to find scripts in your path.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[201,512,28,57,80],"class_list":["post-1525","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-authenticodesignature","tag-get-command","tag-powershell","tag-scripting","tag-scripts"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=1525"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1525\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}