Once you’ve been using PowerShell for a while you start to notice that you can often get the job done using just one line of code. These “one liners” can sometimes look pretty impressive, but they are also get pretty cryptic!
Its a good sales pitch for PowerShell: “yeah, I can do [insert amazing feat here]. It just takes a line of code in PowerShell!” ;)
I like to think of these, as small bragging rights for the new language, and I call them Power Lines. As I find interesting Power Lines during my wanderings I will start posting them here.
Of course the most impressive Power Lines I’ve seen around originate from developers, so I make no promises on the “amazement” factor ;)
Starting off small here’s the first one that I’ve been using today:
[system.guid]::NewGuid().ToString()
This simply generates a new GUID in string format.
A couple of other examples I’ve seen around:
MS PowerShell One Liners
MOW’s old blog
Google
What’s your Power Line?
I’ll take that challenge! Here’s a one liner that returns specified event log entries for the last 14 days from a specified computer:
$d=([System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((get-date).AddDays(-14)));get-wmiobject -query “Select Logfile,Eventcode,TimeGenerated,TimeWritten,Message from win32_ntlogevent where logfile=’System’ AND eventcode=’7036′ AND TimeWritten >=’$d’” -computername “DC01″ | Select Logfile,EventCode,TimeGenerated,Message | format-table -auto
And I am NOT a developer. ;-)
Jeffery Hicks
SAPIEN Technologies – Scripting, Simplified. http://www.SAPIEN.com
VBScript & Windows PowerShell Training – http://www.ScriptingTraining.com/classes.asp
Windows PowerShell? – http://www.SAPIENPress.com/powershell.asp
blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com
Here’s my powerline for making powershell files indexable as text files by Indexing Service
“.ps”,”.ps1″,”.ps1xml”,”.psc1″ | %{New-Item -Path registry::HKEY_CLASSES_ROOT\$_ -Name PersistentHandler | Set-ItemProperty -Name “(Default)” -Value “{5e941d80-bf96-11cd-b579-08002b30bfeb}”}
Nice one guys.
Both pretty much leave mine cold. I’m obviously going to have a think about this ;)
It looks like I was wrong about the developer comment too ;)
Cheers
Adam
While a “Power Line” is a relatively innocent version of “Mine’s bigger”, and kinda fun, I try not to write Powershell expressions this way. For one, they are a little harder to debug. Plus, for other people, they can be very hard to follow. I prefer so set some variables and then combine them into a single expression.
What a good Power Line should demonstrate is the power of the pipeline; how you can take output from one cmdlet and process it through several other cmdlets to come out with a useful and meaningful result.
Jeffery Hicks
SAPIEN Technologies – Scripting, Simplified. http://www.SAPIEN.com
VBScript & Windows PowerShell Training – http://www.ScriptingTraining.com/classes.asp
Windows PowerShell? – http://www.SAPIENPress.com/powershell.asp
blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com
Jeffrey,
I couldn’t agree more. I think of Power Lines as examples of what can be done in one line for fun. They definately don’t represent “best practice”!
Cheers
Adam
I allso agree, most of my powerlines are the result of “fiddling to get it working”, i generally press the up arrow and modify my expression untill i get the results i want. When it comes to saving the expression for future reuse you probably want to add some params and save it in a function.
Besides, powerlines seem to be the opposite of monadology,
big_unreadable_hard_to_debug_process vs. small_comprehensible_debuggable_step.