Export (0) Print
Expand All
Expand Minimize

Windows PowerShell Tip of the Week

Windows PowerShell Tip of the Week


Here’s a quick tip on working with Windows PowerShell. These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know.

Referencing Variables and Variable Values

When you write a script, particularly a system administration script, you rarely get to hard-code in all your values ahead of time; instead, you typically need to retrieve information, store that information in a variable or two, and then display the values of those variable. For example, you might have a simple VBScript script that uses code like this to grab data from Active Directory:



strFirstName = objUser.givenName
strLastName = objUser.sn
strTitle = objUser.title
strDepartment = objUser.department



Once you have that data you’d then like echo back a nice little sentence like this:



"Ken Myer is an Accountant in Financial Services."



No big deal, right? Well, maybe. On the other hand, combining text and variable values can be a bit tricky in VBScript, to say the least. For example, to echo the above information you’ll have to write a line of code that looks like this:



Wscript.Echo strFirstName & " " & strLastName & " is an " & strTitle & " in " & strDepartment & "."



That’s not impossible, but it can be cumbersome, and it’s easy to do things like, say, leave out a blank space. In turn, those simple mistakes result in somewhat less-than-professional output, output similar to this:



"Ken Myer is anAccountant inFinancial Services."



But what are you going to do about it? After all, it’s not as if you can directly embed a variable within a string. Like it or not, all those ampersands and double quote marks are just an unfortunate fact of life.

Well, unless you’re using Windows PowerShell, that is.

Displaying Variable Values in Windows PowerShell

In Windows PowerShell you actually can embed a variable within a string, and PowerShell will display the value of that variable. What does that mean? Well, consider this simple little PowerShell script, one that assigns values to four different variables and then uses the Write-Host cmdlet to echo back a string that includes each of those variables:



$FirstName = "Ken"
$LastName = "Myer"
$Title = "Accountant"
$Department = "Financial Services"

Write-Host "$FirstName $LastName is an $Title in $Department."



Now, what do you suppose will actually get displayed to the screen? Believe it or not, this is what gets displayed to the screen:



Ken Myer is an Accountant in Financial Services.



No ampersands, no double quote marks, no need to worry about adding blank spaces: just include the variable within the string and PowerShell will echo back the value of the variable.

It’s so simple and obvious that it’s revolutionary.

Now, admittedly, there might be times when you don’t want the value of a variable to be displayed to the screen. For example, suppose you wanted this displayed to the screen:



$FirstName $LastName is an $Title in $Department.



Weird, but it’s none of our business what you want to echo back to the screen.

So how are you supposed to do that if PowerShell automatically replaces variables with their values? That’s easy; you just need to enclose the string in single quote marks:



Write-Host '$FirstName $LastName is an $Title in $Department.'



Single quote marks result in literal values being echoed back; double quote marks result in the actual value of a variable being echoed back.

Here’s another one for you. As you know, PowerShell includes a number of “automatic” variables that return information about PowerShell and the PowerShell environment; for example, the $pshome variable returns the name of the folder where Windows PowerShell was installed. Suppose we have this line of code (it’s a weird line of code, but bear with us):



Write-Host "$pshome"



That’s going to return the value of $pshome; that is, the folder where PowerShell was installed:



C:\WINDOWS\system32\WindowsPowerShell\v1.0



Now suppose we have this line of code:



Write-Host '$pshome'



Thanks to the single quote marks that’s going to return the following string (literal) value:



$pshome



Needless to say, that’s all great. But what if we wanted to echo back the following:


The value of the $pshome variable is C:\WINDOWS\system32\WindowsPowerShell\v1.0.



How in the world are we going to do that? How can we echo back both the literal and the actual value of a variable, and in the same sentence to boot?

Here’s how:



Write-Host "The value of the `$pshome variable is $pshome."



See what we’ve done here? We’ve enclosed the entire string in double quote marks; therefore you’d expect PowerShell to echo back the value of each instance of $pshome. But notice the funny little character (the “grave accent”) we placed in front of the first instance of $pshome:



`$pshome



That’s the secret right there: put a grave accent in front of a variable and PowerShell will echo back the variable name ($pshome) rather than the value of that variable. And that’s true even if – as we’ve done here – the entire string is enclosed in double quotes.

In other words, combining variables and other text is easy is PowerShell: just place the variables within a string and PowerShell will, by default, echo back the value of those variables. Admittedly, we’re going to miss ampersands, double quotes, and weird constructions like this:



Wscript.Echo strLastName & ", " &  strFirstName & " " & strInitials & "."



But we’ll try to get by.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft