Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


June 2008

PowerShell 101, Lesson 5

How to access, create, and use variables
RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Different Data Types
Now let’s take a look at some other considerations when using user-defined variables. But first, create $var1 again with the statement

$var1 = "one two"

because you just used the Remove-Variable cmdlet to delete it. As with built-in variable values, user-defined variable values can be retrieved by entering the variable’s name:

$var1
Because you assigned a string value to $var1, PowerShell stores the value as a string object. To verify the object type, you can use the GetType() method:
$var1.gettype()
Figure 2 shows the results of all three of these statements.

You’re not limited to string values when you create a variable. You can just as easily assign an integer:

$var1 = 123; $var1.gettype()
PowerShell automatically stores the value as the correct type, which is Int32, as Figure 3 shows. Notice that this code includes multiple statements. As I discussed in Lesson 2, you can use a semicolon to manually terminate a statement. So, provided you use semicolons, you can put multiple statements on one line. In this case, I’ve included two statements: the first assigns the numeric value, and the second retrieves its type.

Also note that this code assigns a value to the same variable used in the previous set of examples. I did this to demonstrate that the process of creating and updating a variable is the same.

Besides strings and integers, you can assign other types of values, such as

$var1 = 12.34; $var1.gettype()
$var1 = get-date; $var1.gettype()
The first line stores the value as type Double, whereas the second line stores the value as type Date- Time, as Figure 3 shows.

To append text to an existing string value in a user-defined variable, you can follow the same approach I outlined for appending text to an existing string value in an environment variable. For example, the following code assigns a string value to $var1, then appends the string four to it:

$var1 = "one two three"; $var1
$var1 = $var1 + " four"; $var1
Figure 4 shows the results.

Now let’s try a similar operation with numerical values:

$var1 = 123; $var1
$var1 = $var1 + 4; $var1
In this case, PowerShell doesn’t append the number 4, but instead adds 4 to the total amount, as shown in Figure 4. You can, however, define the number as a string by enclosing it in quotes:
$var1 = "123"; $var1
$var1 = $var1 + 4; $var1
The number 4 is now appended to the original value. In fact, you can take this approach with any string:
$var1 = "one two three"; $var1
$var1 = $var1 + 4; $var1
In this case, the results shown in Figure 4 might seem odd, but PowerShell has done exactly what you’ve told it to do.

Although you can append text or a number to a string value, you can’t append text to a numeric value. For example, if you attempt to append the string four to the number 123

$var1 = 123; $var1
$var1 = $var1 + "four"; $var1
you’ll receive an error, as seen in Figure 4. You can append values to an existing value only when the types are compatible. The important point to remember is that PowerShell tries to do the right thing by letting you assign any type of data to a variable as long as it can be converted to the correct type.

Continue on Page 4

   Previous  1  2  [3]  4  Next 


Learning Path To read the previous PowerShell 101 lessons, go to
"PowerShell 101, Lesson 1"

"PowerShell 101, Lesson 2"

"PowerShell 101, Lesson 3"

"PowerShell 101, Lesson 4"


If you're beyond the basics, check out
"User-Friendly Time Spans in Windows PowerShell"

"Changing PowerShell's Internal Functions"


Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


Related Articles How to Handle Long PowerShell Statements

PowerShell Empowerment

PowerShell Script Lets You Check Patches' Status

PowerShell Pointers

Related Events WinConnections and Microsoft® Exchange Connections

Deep Dive into Windows Server 2008 R2 presented by John Savill

PowerShell 201 - eLearning Series with Paul Robichaux

Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement