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

Agile Methodology in Project Management

0 153 0

In this video, you will gain an understanding of Agile and Scrum Master Certification terminologies and concepts to help you make better decisions in your Project Management capabilities. Whether you’re a developer looking to obtain an Agile or Scrum Master Certification, or you’re a Project Manager/Product Owner who is attempting to get your product or … Continue reading Agile Methodology in Project Management

An Overview of Office 365 – Administration Portal and Admin Center

0 842 3

This is part 1 of our 5-part Office 365 free training course. In this Office 365 training video, instructor Spike Xavier introduces some of the most popular services found in Microsoft Office 365 including the Admin Portal and Admin Center. For instructor-led Office 365 training classes, see our course schedule: Spike Xavier SharePoint Instructor – … Continue reading An Overview of Office 365 – Administration Portal and Admin Center

Detailed Forensic Investigation of Malware Infections – April 21, 2015

4 608 5

How does an investigator hunt down and identify unknown malware? In this recording of our IT Security training webinar on April 21, 2015, Security expert Mike Danseglio (CISSP / CEH) performed several malware investigations on infected computers and identify symptoms, find root cause, and follow the leads to determine what’s happening. He demonstrated his preferred … Continue reading Detailed Forensic Investigation of Malware Infections – April 21, 2015

Write a Comment

See what people are saying...

  1. Jason Helmick Jason Helmick

    Ya, I’m still arguing about it in my head. I can see new admins starting with PowerShell maybe using it, but….we will see!

  2. Avatar Mike Shepard

    I completely agree. I like the idea of simplifying the syntax, but if it’s only a “part-way” solution, then everyone has to know the $_ syntax, too. PowerShell is definitely not a one-way-to-do-it solution, but I don’t see how this helps in any big way.

Share your thoughts...

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