{"id":1559,"date":"2009-07-07T08:00:18","date_gmt":"2009-07-07T16:00:18","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2009\/07\/07\/july-powershell-one-liner\/"},"modified":"2009-07-07T08:00:18","modified_gmt":"2009-07-07T16:00:18","slug":"july-powershell-one-liner","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/07\/07\/july-powershell-one-liner\/","title":{"rendered":"July PowerShell One-Liner"},"content":{"rendered":"<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/06\/Search01.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; margin: 0px 5px 5px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" border=\"0\" alt=\"Search01\" align=\"left\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/06\/Search01_thumb.png\" width=\"58\" height=\"120\"><\/a> This month&#8217;s PowerShell one-liner queries the WMI Win32_Product class for a specified computer and builds a formatted report of all installed applications sorted by vendor. You&#8217;ll be able to tell at a glance what is installed, when it was installed, where it was installed from and the product version. Be warned that querying this WMI class may take several minutes or longer.<\/p>\n<p>This one line expression defaults to the local computer, but you can substitute any computername you want. You can also add support for alternate credentials. I&#8217;ve broken the expression into several lines for publication purposes but this is in fact a oneline expression. <\/p>\n<p>This has been tested on both PowerShell v1 and v2.<\/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-WmiObject win32_product -computer $env:computername | <\/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 Vendor,InstallDate | <\/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\">Format-Table -GroupBy Vendor Name,`<\/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\">@{Label=<span style=\"color: #006080\">\"InstallDate\"<\/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\"><span style=\"color: #006080\">\"$($_.InstallDate.substring(4,2))\/$($_.InstallDate.substring(6,2))\/$($_.InstallDate.substring(0,4))\"<\/span>}},`<\/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\">InstallLocation,InstallSource,PackageName,Version -wrap | <\/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\">Out-File productreport.txt<\/pre>\n<\/div>\n<\/div>\n<p>I probably could have left the InstallDate alone since a value like 20090511 isn&#8217;t too hard to figure out, but I took the extra step to parse and reorder so that you get a friendlier value of 05\/11\/2009. Of course if you have different regional settings you might need to tweak my substring parsing.<\/p>\n<p>The oneliner builds a formatted report, but you could easily modify it to use <strong>Select-Object<\/strong> instead so that you could export to a CSV, build an HTML report or whatever you require.<\/p>\n<p>There is a wealth of information in this class. Here are some other ways you might want leverage this class:<\/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\">$products=Get-WmiObject win32_product -computer $env:computername<\/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\">$products | sort InstallDate | Select Name,InstallDate,Vendor,Version<\/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\">$products | group vendor | sort count -descending | Select Count,Name<\/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\">$products | Where {$_.vendor <span style=\"color: #cc6633\">-like<\/span> <span style=\"color: #006080\">\"SAPIEN*\"<\/span>} | Sort Name | Select Name,Version,InstallDate,InstallSource,PackageName<\/pre>\n<\/div>\n<\/div>\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:04a48f82-16f0-4c23-ae26-ec9ed453840f\" class=\"wlWriterSmartContent\">\n<p>Download a script file with these expressions <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/06\/July09-OneLiner.txt\" target=\"_blank\">here.<\/a><\/p>\n<\/div>\n<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s PowerShell one-liner queries the WMI Win32_Product class for a specified computer and builds a formatted report of all installed applications sorted by vendor. You&#8217;ll be able to tell at a glance what is installed, when it was installed, where it was installed from and the product version. Be warned that querying this WMI class may take several minutes or longer.<\/p>\n<p>This one line expression defaults to the local computer, but you can substitute any computername you want. You can also add support for alternate credentials. I&#8217;ve broken the expression into several lines for publication purposes but this is in fact a oneline expression.<\/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":[28,57,516,35],"class_list":["post-1559","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-powershell","tag-scripting","tag-win32_product","tag-wmi"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1559","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=1559"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1559\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}