Powershell – The Power of the “One Liner"

| | Comments (1) | TrackBacks (0)

Powershell

The Powershell environment offers up something truly unique I think.  The concept of what “Shellers” call the “One Liner”

It is truly a geeky thing to behold to see a well written one too.

People new to Powershell may not initially quite grasp just WHY you’d bother with such a thing.   It goes back to that whole concept of “Instant Gratification” in Powershell.

You see, you can type in line by line and test things.  On that particular piece of code.  But to test a BLOCK of things or trying to see what those objects have really can’t happen until “Script Execution time” or unless you begin to the learn the art of the “one liner”.

All a “One Liner” is, is the content of a script written and composed using the most DIRECT form you can.  Let’s go backwards.  Let’s see a script first in normal mode and then as a single line

$TODAY=GET-DATE
$LIST=GET-CHILDITEM C:\STUFF
FOREACH ( $ITEM in $LIST )
{

IF $ITEM.LASTWRITETIME –LT $TODAY.ADDDAYS(-30) {
      WRITE-HOST $ITEM.NAME ‘is an Old file’
      WRITE-HOST ‘-----------‘
}

}

A pretty simple script that looks in the “Stuff” folder for … well stuff that is older than 30 days and lists it.

Now let’s “condense” the lines in the “IF” statement by separate lines instead and remove all the returns

$TODAY=GET-DATE
$LIST=GET-CHILDITEM C:\STUFF
FOREACH ( $ITEM in $LIST )
{

IF $ITEM.LASTWRITETIME –LT $TODAY.ADDDAYS(-30) { WRITE-HOST $ITEM.NAME ‘is an Old file’; WRITE-HOST ‘-----------‘ }

}

See ?  The CODE is still the same but all we’ve done is place a “;” for each line where Code statement would have ended.  Now we’ll go further and remove the blank space between the “{ }”

$TODAY=GET-DATE
$LIST=GET-CHILDITEM C:\STUFF
FOREACH ( $ITEM in $LIST )
{ IF $ITEM.LASTWRITETIME –LT $TODAY.ADDDAYS(-30) { WRITE-HOST $ITEM.NAME ‘is an Old file’; WRITE-HOST ‘-----------‘ } }

Look carefully.  This is STILL the same code.  We’ve removed the FORMATTING but this is still the same code.   Now let’s jump one more level and remove the Line breaks for the rest of the lines.

$TODAY=GET-DATE; $LIST=GET-CHILDITEM C:\STUFF; FOREACH ( $ITEM in $LIST ) { IF $ITEM.LASTWRITETIME –LT $TODAY.ADDDAYS(-30) { WRITE-HOST $ITEM.NAME ‘is an Old file’; WRITE-HOST ‘-----------‘ } }


Ok this is definiitely a lot harder on the eyes, but if you look carefully, the “;” have replaced the line breaks from before.  That’s because they both serve as a “Delimiter” which is one of those funky Developer words that mean “STOP HERE!”

And now to get really “l33t” on this One Liner, we’ll remove the variables where we don’t REALLY need them and shorten what we can.

$TD=GET-DATE; GET-CHILDITEM C:\STUFF; FOREACH ( $_ ) { IF $_.LASTWRITETIME –LT $TD.ADDDAYS(-30) { WRITE-HOST $ITEM.NAME ‘is an Old file’; WRITE-HOST ‘-----------‘ } }

Ok, yes hard on the eyes.  not really more efficient.  But if you understand the format of how you CAN execute an entire shell in one line, then sometimes you can use Powershell to quickly AdHoc query information, composing quick little scripts.

Sometimes just to see if it will work. :)

Powershell, One Liners there for you

Sean
The Energized Tech

0 TrackBacks

Listed below are links to blogs that reference this entry: Powershell – The Power of the “One Liner".

TrackBack URL for this entry: http://www.energizedtech.com/cgi-sys/cgiwrap/jolyrogr/managed-mt/mt-tb.cgi/313

1 Comments

dir C:\stuff | % { if($_.LastWriteTime –lt (get-date).AddDays(-30)) { $_.Name + " is an Old file`n-----------" } }

Leave a comment