Prof. Powershell

PSForecast: Variable with a Chance of Objects

Variables hold a special place in ... er ... your scripts; namely, they're placeholders for objects.

Whether using PowerShell interactively in the console or through a PowerShell script, you are most likely working with objects:

PS C:\> get-service spooler

This expression will return a System.ServiceProcess.ServiceController object representing the Spooler service. This object has a number of properties you might want to access. If you want to repeatedly access this object, the best approach is to create a variable to hold it. A variable is a special type of object that simply holds other objects. It's a chameleon that takes on the nature of the underlying object.

Creating a variable is as easy as using the assignment operator:

PS C:\> $spool=gsv spooler

If we display $spool, we'll get the default view of the Spooler service object:

PS C:\> $spool

Status   Name     DisplayName
------   ----     -----------
Running  spooler  Print Spooler

We can also easily display properties using a variable:

PS C:\> $spool.RequiredServices

Status   Name   DisplayName
------   ----   -----------
Running  RPCSS  Remote Procedure Call (RPC)
Running  HTTP   http

PowerShell is actually full of variables, many of which are created automatically when you start a PowerShell session. Use the Get-Variable cmdlet to display all currently defined variables. The cmdlet has an alias of gv:

PS C:\> gv

I'll let you try it out to see the results for yourself. You can also look at a specific variable. You can see similar results by listing the contents of the Variable: PSDrive this way:

PS C:\> dir variable:

To look at a specific variable, use Get-Variable and pass the variable name as a parameter:

PS C:\> gv home

Name  Value
----  -----
HOME  C:\Users\Jeff

You're probably wondering why I didn't use $home. I only need the $ sign when I am accessing the variable's value:

PS C:\> $home
C:\Users\Jeff

Variables actually have other information you might need to see:

PS C:\> gv home | select *

Name        : HOME
Description : Folder containing the current user's profile.
Value       : C:\Users\Jeff
Visibility  : Public
Module      :
ModuleName  :
Options     : ReadOnly, AllScope
Attributes  : {}

Variables are subject to the rules of scope, so this information can be very helpful when troubleshooting a script or complex PowerShell expression.

Remember that variables will remain as long as your PowerShell session is running, or until you redefine them. If you explicitly want to get rid of a variable, use the Remove-Variable cmdlet:

PS C:\> Remove-Variable spool

Again, I don't use the variable as an object. The Remove-Variable cmdlet is expecting a string for the variable name. You could also delete the variable “file” from the Variable: PSDrive, but I find the cmdlet much easier to use.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.