Skip to main content

$This, $_, $PSItem, $Whatever ????

In my PowerShell classes, we explore multiple variables to help us work with different cmdlets and constructs. I often get questions on when to use special variables. Take a look below for an explanation of when to use some of them.

$_

This is the generic variable used in PowerShell prior to V3. I still use it because it is shorter and helps prevent horizontal scrolling. We use it to represent the object that is currently in the PowerShell Pipeline. When you pass information in the pipeline, only one object will be in the pipeline. This variable represents that object. We do not provide a named variable in the pipeline. $_ will have access to all of the members of that object.

# This will show you that $_ is the object in the pipeline.

# $_ is a STRING object.

 

Get-ChildItem |

    Select-Object -First 1 -ExpandProperty Name |

    ForEach-Object {

        $_

    } 

You have access to the objects properties.

# Accessing the objects properties.

 

Get-ChildItem |

    Select-Object -First 1 -ExpandProperty Name |

    ForEach-Object {

        $_.Length

    }

You also have access to the objects methods.

# Accessing the objects methods.

 

Get-ChildItem |

    Select-Object -First 1 -ExpandProperty Name |

    ForEach-Object {

        $_.ToUpper()

    }

 

$PSITEM

$PSItem was introduced in PowerShell V3. The idea of introducing $PSItem is to help make your code more readable. Both $_ and $PSItem stand for “The current object in the PowerShell Pipeline.” Those of you who have sat in my class may recall that I say those exact words when I type either one of these. There is no difference between the two except the $PSItem is only supported in PowerShell V3 and later.

 

Generic variable used with ForEach looping.

When we are not using the PowerShell Pipeline, but need to loop through an unknown number of objects, we use the ForEach construct. Take a look at this code.

# Looping with ForEach

 

$Events = Get-EventLog -LogName System -Newest 5

ForEach ($Event in $Events)

{

    $Event.instanceId

}

First we gather the objects that we want to work with. In this case, we are collecting event objects. In the declaration of the ForEach loop, we declare a temporary variable called $Event. A lot of documentation uses the singular form of the variable holding all of the objects for the generic variable. You can see that $Events is holding the collection of objects and $Event is the generic variable used to reference the objects in $Events. I find this to be to confusing. You can name the generic variable anything that you want. I am going to change it to $E for clarity.

# Looping with ForEach

# Shortened generic variable for clarity.

 

$Events = Get-EventLog -LogName System -Newest 5

ForEach ($E in $Events)

{

    $E.instanceId

}

 

$This

This is a bit more complicated to explain. I’m actually adding the CLASS keyword to my advanced PowerShell classes. PowerShell 5 supports the creation of classes just like many other object orientated languages. The $This variable refers to the object itself. We use it when creating methods or modifying the properties of the object being accessed.

# Using $This in a method.

Class MyMath

{

    $X = 5

    $Y = 10

    $Total = 0

 

    [Void]Add()

    {

        $This.Total = $This.X + $This.Y

    }

}

 

$Math = [MyMath]::new()

$Math

$Math.Add()

$Math

These variables can be a bit confusing. It is part of learning PowerShell’s syntax. I’ve been asked, “Why do you have to type this stuff?” Well, that is coding. We need to clearly define what we want the computer to do. One of my college professors, Dave Syler, told us the computer will do exactly what you tell it to do. If you do not provide clear instructions, it will go off on its own and do something else. The syntax is designed to provide clear instructions.

I hope this helps many of you out. Once you have used the syntax for a while, it becomes second nature to you.

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.