RSS All Posts

RSS PowerShell Posts

Tags

2142 Active Directory Administrativia Announcements Battlefield Blogging Cricket Deployment Deployment4 Get-PSUGUK Group Policy HowTo Linux MDT MDT 2010 Microsoft Deployment Toolkit MSDN Music Permissions Personal PowerGui Power Lines PowerShell PowerShell Groups PowerShell Support PowerShell Tools PowerShell V2 Presentations PSUGAU Quick Tips Scripting SDDL Security Tech Talk Ubuntu User Groups Virtualisation VMware Infrastructure Client WAIK Weekly Poll Windows 7 Windows Automation Installation Kit Windows Server 2003 Windows Server 2008 XML

Archives

Meta


« | Main | »

Power Lines

By Adam Bell | April 4, 2007

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?

Topics: PowerShell | 6 Comments »

6 Responses to “Power Lines”

  1. Jeffery Hicks Says:
    April 4th, 2007 at 13:59

    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

  2. Joris Says:
    April 4th, 2007 at 16:48

    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}”}

  3. AdamBell Says:
    April 4th, 2007 at 17:30

    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

  4. Jeffery Hicks Says:
    April 4th, 2007 at 18:12

    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

  5. AdamBell Says:
    April 4th, 2007 at 18:27

    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

  6. Joris Says:
    April 6th, 2007 at 15:52

    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.

Comments