To $_ or not to $_, that is the question! Windows 8 and PowerShell v3.

Home > Blogs > PowerShell > To $_ or not to $_, that is the question! Windows 8 and PowerShell v3.

To $_ or not to $_, that is the question! Windows 8 and PowerShell v3.

Like This Blog 2 Jason Helmick
Added by November 8, 2011

(This post is based on the current Developers preview build of Windows 8 Server.)

So I’m sitting here contemplating something cool from Win 8 Server and PowerShell V3. I don’t know if you have had a chance to look at Windows 8 Server, perhaps the PowerShell v3 CTP, but you might have noticed this….

Old way:
PS>Get-WmiObject Win32_Service | where{$_.status -eq "Stopped"}
New way:
PS>Get-WmiObject Win32_service | where status -eq stopped

Notice that the Where-Object cmdlet does not require all the “code” looking stuff from version 2. There’s no {}, and no $_.

This is really cool, especially for an Admin. Have you tried to teach someone else what the $_ is and how to work with it? Sure, developers get it immediately, but Admins have a hard time understanding objects, properties and methods.

I know that the PowerShell Team spends a lot of cycles trying to abstract programming away from Administrators. In fact, I spent a couple of hours talking to Lee Holmes about this at the last PowerShell Deep Dive. The amount of passion the Team has to make it easy for Administrators to use PowerShell without learning to be programmers is amazing. This is a good example of that passion at work.

So, expanding off this wonderful idea, I tried this:
PS>Get-WmiObject Win32_Service | where status -eq Stopped -
and Startmode -eq Auto

Windows 8 and PowerShell v3 error message Get-WmiObject Win32_Service

Oops. Didn’t like that now, did she? (P.S. Read the error message. For fun, go look at the full help on Where-Object and you can glean how the Team is making this magic happen and why it’s not working.)

Let me say first that I understand this is a Preview release, not even beta, and the PowerShell Team is hard at work, and this could change.

As I consider the “new-way,” but see the limitation, I get into a funky mood and ask myself, “Is this really a good idea?” My friends refer to these moods as “disaster-bating,” and they generally leave the room when I start a discussion, but here it is:

Last week, I was teaching PowerShell V2 to Administrators and at the end of class I demo’d the Win 8 missing $_ trick. At first the response was pretty cool until we started to play with it. As soon as I did this…
PS>Get-WmiObject Win32_Service | where status -eq Stopped -
and Startmode -eq Auto

Everyone seemed a little crest fallen. This still works though;
PS>Get-WmiObject Win32_Service | where {$_.status -eq
"Stopped" -and $_.Startmode -eq "Auto"}

Now, the funny thing is Microsoft might be able to pull this off and remove $_ from the pipeline and our one-liners, but the concept of an object still remains. Frankly, when Admins start automating and making scripts they will end up dealing with $_ and objects anyway. So…

I applaud the team for abstracting as much “programming” or “development” from the Admin as possible. If the feature can be fully implemented so that $_ is not needed, great! If it can only be a feature for a few things, if it only works under “certain” conditions, then remove it.

Knowledge is PowerShell,

Jason

Videos You May Like

A Simple Introduction to Cisco CML2

0 3638 0

Mark Jacob, Cisco Instructor, presents an introduction to Cisco Modeling Labs 2.0 or CML2.0, an upgrade to Cisco’s VIRL Personal Edition. Mark demonstrates Terminal Emulator access to console, as well as console access from within the CML2.0 product. Hello, I’m Mark Jacob, a Cisco Instructor and Network Instructor at Interface Technical Training. I’ve been using … Continue reading A Simple Introduction to Cisco CML2

How to Build in a PSMethod to your PowerShell Code

0 65 0

In this video, PowerShell instructor Jason Yoder shows how to add Methods (PSMethod) to your code using free software that’s added into the PSObject. For instructor-led PowerShell courses, see our course schedule. Microsoft Windows PowerShell Training Download the Building Methods PowerShell script</a> used in this video. <# ╔══════════════════════════════════════════════════════════════════════════════╗ ║ ║ ║ Building Methods ║ ╟──────────────────────────────────────────────────────────────────────────────╢ … Continue reading How to Build in a PSMethod to your PowerShell Code

How to use the PowerShell Script Analyzer to Clean Up Your Code

0 1211 2

In this video, PowerShell instructor Jason Yoder demonstrates how you can use the PowerShell Script Analyzer to help you format your code to best-practices. For instructor-led PowerShell training classes, see our course schedule: Microsoft Windows PowerShell Training Download the PowerShell Analyzer scripts used in this video. <# ╔══════════════════════════════════════════════════════════════════════════════╗ ║ ║ ║ PowerShell Script Analyzer ║ … Continue reading How to use the PowerShell Script Analyzer to Clean Up Your Code

Write a Comment

See what people are saying...

    Share your thoughts...

    Please fill out the comment form below to post a reply.