Difference Between $_ and $PSItem in Windows PowerShell

Home > Videos > PowerShell > Difference Between $_ and $PSItem in Windows PowerShell

Difference Between $_ and $PSItem in Windows PowerShell

Like This Video 1 363Jason Yoder
Added by April 2, 2018

In this video, I’m going to answer a very common question that I get when I’m teaching Windows PowerShell, and that’s, “What’s the difference between what’s called $_ and $PSItem?” Let me explain to you a little bit about what these two variables do.

When we’re working with the PowerShell pipeline, and we want to reference whatever object is currently moving through the pipeline, we need a way of doing this. There’s two automatic variables that we can utilize, $_ or $PSItem. Let me show you.

I’m going to run this command. All it’s going to do is grab three objects, representing each one of the drives on this particular machine. What I’m going to do is I want to reference just the device ID property. You can see over here, the device ID property is simply going to be my drive letters when we’ve finished.

What I’m going to do is, first, do it with $_. You can see I’m taking my original command. I’m piping it before each object, and I’m referencing the current object in the PowerShell pipeline ‑‑ and that is how we say it ‑‑ then we’re going to reference its device ID property.

Let me run this one first. You can see all we get are the drive letters. Now I’m going to run this again, but this time I’m going to replace $_ with $PSItem. That’s the only difference in this code. We get the exact same thing. Let’s discover why. I’m going to go ahead and open up the help file about automatic variables.

In this help file, if I scroll down a little bit, here’s the reference to $_. Right there, it’s telling you it’s the same as $PSItem. It contains the current object in the PowerShell pipeline. Let’s take a look at $PSItem. Let’s jump down here. Look, $PSItem, the same as $_, so there’s some confusion in the PowerShell world. When should I actually use one or the other?

I’m going to be honest with you. They are exactly the same. [laughs] In PowerShell 2, when we use $_, it started to cause some confusion, so when PowerShell 3 was released the creators of PowerShell decided, “Let’s go ahead and rename it, or provide an alternate name for it, PSItem.” For some individuals, it’s a little more clear as to what it represents.

For me, personally, I still use $_ to save room on the screen, particularly when I have to teach and project it on a big screen for everybody to see, I need to be conscious of how much horizontal real estate I have. Either one is supported and will work.

The next question I usually get is, “Well, which one’s faster?” What I’m going to do is I’m going to run a little speed test here for you. We are running inside of a virtual machine, and it’s actually in a shared environment. It’s up in Azure, so the results will vary here. I’m going to run this test several times.

Essentially, what it’s going to do is it’s going to do a measurement of the same procedure using $_ and $PSItem. It’s going to run each one of these tests 100 times and give me the average of the number of ticks.

If you’re not familiar with a date‑time object, a tick is an extremely minute small amount of time. It’s the most accurate measurement we can come up with. Let’s go ahead and run this.

The items in green is the faster of the two. You can see we’re pretty much on an even spread. The reason is that these are exactly the same thing. There is no performance difference between the two of them. No matter how many times I run this, almost always do we get an even spread between them.

Which one should you use? That’s up to you and whatever team you’re working with. If your team prefers $PSItem, go ahead and use that. $_, it’s still there for backward compatibility and you can still use it.

For instructor-led PowerShell training classes, see our course schedule:

Microsoft Windows PowerShell Training

PowerShell $_ and $PSItem in Windows PowerShell  used in this video.

Download the $_ and $PSItem in Windows PowerShell Script.


Videos You May Like

Agile Methodology in Project Management

0 93 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

JavaScript for C# Developers – September 24, 2014

0 322 3

In this video Dan Wahlin demonstrate the similarities and differences between C# and JavaScript and the future of JavaScript (ES6).

Detailed Forensic Investigation of Malware Infections – April 21, 2015

3 360 3

In this IT Security training video, Security expert Mike Danseglio (CISSP / CEH) will perform several malware investigations including rootkits, botnets, viruses, and browser toolbars.

Write a Comment

See what people are saying...

  1. Avatar Amazed viewer

    Wow, I must say that I am amazed at how you managed to create a min video on how $_ is an alias for $PSItem.

Share your thoughts...

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