PowerShell Data types
PowerShell, being built on the .NET Framework, supports a wide range of data types from the .NET ecosystem. Here are some commonly used data types in PowerShell:
-
Scalars (simple types):
- [string]: Represents a sequence of characters.
- [int] or [System.Int32]: Represents a 32-bit signed integer.
- [long] or [System.Int64]: Represents a 64-bit signed integer.
- [bool] or [System.Boolean]: Represents a boolean value (true or false).
- [char] or [System.Char]: Represents a single Unicode character.
- [float] or [System.Single]: Represents a single-precision floating-point number.
- [double] or [System.Double]: Represents a double-precision floating-point number.
- [decimal] or [System.Decimal]: Represents a high-precision decimal number, often used for financial calculations.
-
Collections:
- Array: A fixed-size, zero-based, ordered collection of elements. Arrays can hold elements of any data type, including other arrays or mixed data types.
- [System.Collections.ArrayList]: A dynamic array that can be resized during runtime.
- [System.Collections.Hashtable]: A collection of key-value pairs that can be accessed by a unique key.
- [System.Collections.Generic.List[T]]: A strongly typed, dynamic list of elements of type T.
- [System.Collections.Generic.Dictionary[TKey, TValue]]: A strongly typed collection of key-value pairs with unique keys.
-
Objects:
- [System.Object]: The base type for all .NET objects, including custom objects.
- [PSCustomObject]: A custom object in PowerShell, typically created with the New-Object cmdlet or by using a hashtable with the [PSCustomObject] type accelerator.
- [PSObject]: A wrapper around .NET objects that adds additional properties and methods, used extensively in PowerShell.
-
Special types:
- [System.IO.FileInfo]: Represents a file in the file system.
- [System.IO.DirectoryInfo]: Represents a directory in the file system.
- [System.DateTime]: Represents a date and time value.
- [System.TimeSpan]: Represents a time interval.
- [System.Net.IPAddress]: Represents an IP address (IPv4 or IPv6).
PowerShell allows you to create and manipulate variables of these data types and many more. You can also create custom types using classes, which are available in PowerShell 5.0 and later.
Working with Strings:
See the following examples. We are assigning a string to variable $a.
PS C:\> $a = "I am a string"
PS C:\> $a.GetType().FullName
System.String
PowerShell automatically assigned the variable type to System.String . Now let us play with strings
PS C:> $a = "One"
PS C:> $b = "Two"
PS C:> $a + $b
OneTwo
PS C:> "$a$b"
OneTwo
PS C:> $a$b
At line:1 char:3
+ $a$b
+ ~~
Unexpected token '$b' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
$a$b has thrown some error. We cannot use like this. If you want to print both the variables, use them in “”.
Now, what will happen if you use single quotes? (‘). Let us see..
PS C:\> '$a$b'
$a$b
Single quote marks result in literal values being echoed back; double quote marks result in the actual value of a variable being echoed back.
Working with Numbers:
PS C:\> $a = 1234; $a
1234
PS C:\> $a.GetType().FullName
System.Int32
PS C:\> $a = 12345678910; $a
12345678910
PS C:\> $a.GetType().FullName
System.Int64
PS C:\> $a = 1234.5678; $a
1234.5678
PS C:\> $a.GetType().FullName
System.Double
PS C:\> $a=1234.5678d; $a
1234.5678
PS C:\> $a.GetType().FullName
System.Decimal
PS C:\>
Converting Data types:
PS C:\> $a = [double] "1234"
PS C:\> $a.GetType().FullName
System.Double
PS C:\> $a = [decimal] "1234"
PS C:\> $a.GetType().FullName
System.Decimal
Working with Arrays:
A PowerShell array holds a list of data items.
Creating Arrays
To create an Array just separate the elements with commas or using explicit syntax:
PS C:\> $myArray = "Hello","World", "One", 1, 1.5, "Hello World"
PS C:\> $myArray
Hello
World
One
1
1.5
Hello World
PS C:\> $myArray = @("Hello","World", "One", 1, 1.5, "Hello World")
PS C:\> $myArray
Hello
World
One
1
1.5
Hello World
range operator (..):
PS C:\> $myArray = (10..15)
PS C:\> $myArray
10
11
12
13
14
15
PS C:\>
Create an empty array:
$myArray$ = @()
Add values to an Array. This is done using the += operator
$countries += 'India'
Retrieve items from an Array:
PS C:\> $myArray = "Hello","World", "One", 1, 1.5, "Hello World"
PS C:\> $myArray[0]
Hello
PS C:\> $myArray[4]
1.5
PS C:\> $myArray[5]
Hello World
Length of Array:
PS C:\> $myArray.Count
6
Searching array:
PS C:\> $arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
PS C:\> $arrColors -contains "black"
False
PS C:\> $arrColors -contains "orange"
True
Working with Hash Tables:
Hash tables are collections that take the System.Collections.Hashtable data type. The primary difference between hash tables and arrays is that hash tables use named keys rather than index numbers to identify values.
Hash Tables are one of the most flexible datatypes supported in PowerShell.
We create a hash table by using the @ symbol followed by a set of braces that enclose the collection of key/value pairs.
PS C:\> $a = @{a="Birds"; b="Animals"; c="Mountains"; d="Oceans"}
PS C:\> $a
Name Value
---- -----
c Mountains
d Oceans
b Animals
a Birds
PS C:\> $a.a
Birds
PS C:\> $a.d
Oceans
PS C:\> $name = @{FirstName = "My FirstName"; LastName = "My LastName";}
PS C:\> $name
Name Value
---- -----
LastName My LastName
FirstName My FirstName
PS C:\> $name.FirstName
My FirstName
PS C:\> $name.LastName
My LastName