June ’09 PowerShell One-Liner

In case you don’t get the SAPIEN Newsletter, here’s my PowerShell one-liner for the month.

The Get-Command cmdlet can return information not only about cmdlets but other applications and scripts that PowerShell “knows” about. This month I have a PowerShell one-liner that uses Get-Command to find all PowerShell scripts in any folder in your path. Using a series of Select-Object 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 Get-AuthenticodeSiganture. You do sign your production scripts don’t you? I’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’ve split it up for legibility.

Get-Command -commandtype externalscript | 
select Name,@{name="Folder";expression={
Split-Path $_.definition -parent}},@{
name="Size";Expression={
(Get-ChildItem $_.definition).length}},@{
name="Created";Expression={
(get-childitem $_.definition).CreationTime}},@{
name="Modified";Expression={
(get-childitem $_.definition).LastWriteTime}},@{
name="Signature";Expression={
(Get-AuthenticodeSignature $_.definition).Status }} |
Sort Folder,Size

Here’s a sample of what you can expect:

Name      : Initialize-VIToolkitEnvironment.ps1
Folder    : C:\Program Files (x86)\VMware\Infrastructure\VIToolkitForWindows\Scripts
Size      : 7749
Created   : 1/20/2009 9:01:34 PM
Modified  : 1/20/2009 9:01:34 PM
Signature : Valid

Name      : voicedemo.ps1
Folder    : C:\Windows
Size      : 82
Created   : 12/23/2008 3:13:51 PM
Modified  : 12/4/2006 1:53:32 PM
Signature : NotSigned

To take this further I would turn this into a function or scriptblock to make it easy to reuse.

Download a copy of this oneliner here.