Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 



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 


Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Command Prompt Tricks

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

New Microsoft/Yahoo! Deal? No

On Sunday, the Times of London reported that Microsoft had renewed talks with failing Internet giant Yahoo! and would manage its search engine for 10 years, while Yahoo! would retain control of its email, messaging, and content services. This report ...

How can I stop and start services from the command line?

...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events SQL Server 2008 – Can You Wait? | Philadelphia

PowerShell 101 - eLearning Series

SQL Server 2008 – Can You Wait? | Atlanta

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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing