Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

February 23, 2009 12:00 AM

Working with PowerShell's Data Types

Lesson 4 in the PowerShell 201 series explores how to work with strings, arrays, and other types of data
Windows IT Pro
InstantDoc ID #101150
Rating: (0)
Executive Summary:

Windows PowerShell uses the Microsoft .NET Framework data types. Learn how PowerShell assigns and converts the System.String, System.Int32, System.Int64, System.Double, System.Decimal, System.Object\[], and System.Collections.Hashtable data types. Also learn how to cast and convert data to specific data types when needed.


In Windows PowerShell, the data your statements retrieve, pass to other statements, and output to the console must conform to the Microsoft .NET Framework data types. For the most part, PowerShell automatically assigns and converts data to its correct data type. However, at times, you might want to control this process to create statements that better use the data you have available. Let's look at how to work with several types of data and how to cast and convert data to specific data types when needed.

Working with Strings

Administrators often work with data that's in the form of strings, and working with strings is a straightforward process in PowerShell. For example, the following code assigns the string "cat" to the $a variable, then displays the result, which is cat:

$a = "cat"; $a 

Because the code assigns a string to $a, PowerShell automatically casts $a with the System.String type. You can use the GetType method and its FullName property to determine a variable's data type. For example, the command

$a.GetType().FullName 

returns the result System.String. To add strings together, you use the plus (+) operator. For instance, the following code adds the string " & dog " (including spaces) to $a, then displays the result, which is cat & dog:

$a = $a + " & dog "; $a 

You can add a numerical value to a string. When you do this, the number is automatically converted to a string. For example, the following code adds 10 to $a, then displays the result, which is cat & dog 10:

$a = $a + 10; $a 

If you were to verify the data type, it would still be String. For more information about working with strings, see "PowerShell 101, Lesson 4." 

Working with Numbers

Like string data, numerical data is simple to work with in PowerShell. You assign numerical values as you would string values, except that you don’t enclose numerical values in quotes. When you specify a numerical value, PowerShell automatically assigns one of four data types to that value (unless you’re concatenating the numerical value to a string, as you saw in the previous example):

  • System.Int32, which is a 32-bit integer ([int] alias).
  • System.Int64, which is a 64-bit integer ([long] alias).
  • System.Double, which is an 8-byte, floating-point decimal ([double] alias).
  • System.Decimal, which is a 12-byte decimal ([decimal] alias). Decimal provides more precision than Double.

The following statements demonstrate how PowerShell assigns data types to numerical values:

$a = 1234; $a
$a.GetType().FullName
$a = 12345678910; $a
$a.GetType().FullName
$a = 1234.5678; $a
$a.GetType().FullName
$a = 1234.5678d; $a
$a.GetType().FullName 

As Figure 1 shows, PowerShell assigns a data type to the first three variables based on the value.

However, to assign the Decimal type, you must specify the letter d directly after the number; otherwise, PowerShell treats the value as a Double type. You can use the plus operator to join numerical values. When you do so, the numerical values are added, not concatenated like they are with string values. For example, the following statements assign 1234 to $a, then add 1.5678 to $a:

$a = 1234; $a
$a.GetType().FullName
$a = $a + 1.5678; $a
$a.GetType().FullName 

As Figure 2 shows, PowerShell adds the two values together and automatically converts the variable’s data type from Int32 to Double.

If you try to add a string that isn't a number to a numerical value, you'll receive an error. PowerShell can't convert nonnumeric string values into numerical values. For example, you can add the string "4" to a numerical value, but you can't add "four".

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.