PowerShell Data Types

If you are starting PowerShell and want to become an expert in it, you should have a complete understanding of PowerShell data types. In this PowerShell tutorial, I will show you various data types in PowerShell, including integers, floats, doubles, decimals, characters, strings, Booleans, dates, arrays, hash tables, and custom objects.

I will also show you how to check the data type in PowerShell.

Various Data Types in PowerShell

Here are a few data types in PowerShell with examples.

1. Integer

Integers are whole numbers without any fractional component. They can be positive or negative. In PowerShell, integers are typically of the type [int], which corresponds to a 32-bit signed integer in the .NET framework.

Syntax

[int]$integerVariable = 10

Examples

Here is a complete example.

[int]$positiveInteger = 42
[int]$negativeInteger = -42

# Arithmetic operations
$sum = $positiveInteger + $negativeInteger
Write-Output $sum

You can see the output in the screenshot below:

Data Types in PowerShell

2. Float and Double

Floats and doubles represent numbers with fractional components. Floats (single precision) and doubles (double precision) differ in the amount of precision and range they offer. Floats are generally used when less precision is acceptable, while doubles are used for more accurate calculations.

Syntax

Here is the syntax for float and double data types in PowerShell.

[float]$floatVariable = 10.5
[double]$doubleVariable = 10.5

Examples

Here is an example.

[float]$floatNumber = 3.14
[double]$doubleNumber = 3.141592653589793

# Arithmetic operations
$floatSum = $floatNumber + 2.86
$doubleSum = $doubleNumber + 2.86
Write-Output $floatSum  # Output: 6.00000010490417
Write-Output $doubleSum  # Output: 6.001592653589793

When I executed the script using VS code and the output, you can see the output in the screenshot below:

PowerShell Data Types

3. Decimal

Decimals are used for high-precision arithmetic, especially useful in financial calculations where rounding errors can be unacceptable. The [decimal] type in PowerShell provides a higher precision and a smaller range of values compared to floats and doubles, making it ideal for currency and other precise calculations.

Syntax

Here is the syntax for decimal data types in PowerShell.

[decimal]$decimalVariable = 10.5

Examples

Here is an example.

[decimal]$decimalNumber = 123.4567890123456789012345678

# Arithmetic operations
$decimalSum = $decimalNumber + 0.5432109876543210987654321
Write-Output $decimalSum 

Read How to Handle Errors with Try-Catch in PowerShell?

4. Character

A character data type in PowerShell represents a single character, such as a letter, digit, or symbol. In PowerShell, characters are of the type [char], which corresponds to a Unicode character. This type is useful for handling individual characters in strings, performing character-level operations, and working with text data.

Syntax

Here is the syntax of character data type in PowerShell.

[char]$charVariable = 'A'

Examples

[char]$charA = 'A'
[char]$charB = 'B'

# Concatenation
$charConcat = "$charA$charB"
Write-Output $charConcat

You can see the output in the screenshot below:

PowerShell data types examples

5. String

Strings are sequences of characters used to represent text. In PowerShell, strings are of the type [string]. Strings support a wide range of operations, including concatenation, substring extraction, and pattern matching.

Syntax

Here is the syntax:

[string]$stringVariable = "Hello, World!"

Examples

Here is an example.

[string]$greeting = "Hello"
[string]$name = "World"

# String concatenation
$fullGreeting = "$greeting, $name!"
Write-Output $fullGreeting  # Output: Hello, World!

# String length
$stringLength = $fullGreeting.Length
Write-Output $stringLength  # Output: 13

6. Booleans

Booleans represent true or false values for logical operations and control flow. In PowerShell, Booleans are of the type [bool]. They are essential for making decisions in scripts, such as conditional statements and loops, which allow scripts to execute different code paths based on certain conditions.

Syntax

Here is the syntax for booleans of data types in PowerShell.

[bool]$booleanVariable = $true

Examples

Here is a simple example of how to use boolean data types in PowerShell.

[bool]$isTrue = $true
[bool]$isFalse = $false

# Conditional check
if ($isTrue) {
    Write-Output "This is true."
} else {
    Write-Output "This is false."
}

I executed the above PowerShell script, and you can see the output in the screenshot below:

boolean data types in PowerShell

7. Dates

Dates represent date and time values. In PowerShell, dates are of the type [datetime], which provides various methods and properties for manipulating date and time data.

Syntax

Here is the syntax

[datetime]$dateVariable = Get-Date

Examples

Here is an example.

[datetime]$currentDate = Get-Date
Write-Output $currentDate  # Output: Current date and time

# Specific date
[datetime]$specificDate = [datetime]"2024-01-01"
Write-Output $specificDate  # Output: 01 January 2024 00:00:00

8. Arrays

Arrays are collections of items of the same or different types stored in a single variable. In PowerShell, arrays are of the type [array]. They are useful for storing lists of data, such as numbers, strings, or objects. They also provide various methods for accessing, adding, and manipulating elements.

Syntax

Here is the syntax:

[array]$arrayVariable = @(1, 2, 3, 4, 5)

Examples

Here is a simple example of how to use the array data type in PowerShell.

[array]$numbers = @(1, 2, 3, 4, 5)
Write-Output $numbers  # Output: 1 2 3 4 5

# Accessing elements
$firstNumber = $numbers[0]
Write-Output $firstNumber  # Output: 1

# Adding elements
$numbers += 6
Write-Output $numbers  # Output: 1 2 3 4 5 6

9. Hash Tables

Hash tables are collections of key-value pairs, similar to dictionaries in other programming languages. In PowerShell, hash tables are of the type [hashtable]. Hash tables store data that can be quickly accessed via a unique key. They are useful for configuring settings, mapping data, and storing structured information.

Syntax

Here is the syntax:

$hashTable = @{ Key1 = 'Value1'; Key2 = 'Value2' }

Examples

Here is a complete example of how to use the hashtable data type in PowerShell.

$person = @{
    Name = "John"
    Age = 30
    Occupation = "Developer"
}

# Accessing values
$name = $person["Name"]
Write-Output $name  # Output: John

# Adding key-value pairs
$person["Country"] = "USA"
Write-Output $person

You can see the output in the screenshot below:

powershell data type hashtable

10. Custom Objects

Custom objects allow you to create structured data by defining properties. In PowerShell, you can create custom objects using the New-Object cmdlet or by using a more modern and concise syntax with [PSCustomObject].

Syntax

Here is the syntax of custom objects data type in PowerShell.

$customObject = New-Object PSObject -Property @{
    Property1 = 'Value1'
    Property2 = 'Value2'
}

Examples

Here is a simple example to understand about the PowerShell custom objects data type.

$person = New-Object PSObject -Property @{
    Name = "Jane"
    Age = 28
    Occupation = "Designer"
}

# Accessing properties
Write-Output $person.Name  # Output: Jane

# Adding properties
$person.Country = "Canada"
Write-Output $person 

How to Check Data Type in PowerShell?

Now, let me show you how to check data type in PowerShell with a few examples.

In PowerShell, you can determine the data type of a variable by using the GetType() method. This method provides detailed information about the variable type, such as its name and base type. Here’s how you can use it:

  1. Assign a value to a variable:$x = 10
  2. Use the GetType() method to find out the type:$x.GetType()

Here are a few examples to understand this better.

Example 1: Integer Type

The below PowerShell script is to know about the integer type in PowerShell.

$x = 10
$xType = $x.GetType()
Write-Output $xType

Output:

IsPublic IsSerial Name     BaseType
-------- -------- ----     --------
True     True     Int32    System.ValueType

In this example, $x is of type Int32, which is an integer.

Example 2: String Type

Here is an example for a string data type in PowerShell.

$str = "Hello, PowerShell!"
$strType = $str.GetType()
Write-Output $strType

Output:

IsPublic IsSerial Name     BaseType
-------- -------- ----     --------
True     True     String   System.Object

Here, $str is of type String.

Example 3: Array Type

Here is an example for a PowerShell array data type.

$arr = @(1, 2, 3)
$arrType = $arr.GetType()
Write-Output $arrType

Output:

IsPublic IsSerial Name     BaseType
-------- -------- ----     --------
True     True     Object[] System.Array

In this case, $arr is an array of objects.

Conclusion

I hope you now have an idea of the various data types in PowerShell. I have shown you the syntax and examples of each data type. Here is a summary:

  • Integers are used for whole number arithmetic.
  • Floats and Doubles handle fractional numbers with varying precision.
  • Decimals are crucial for high-precision arithmetic, especially in financial contexts.
  • Characters represent single Unicode characters.
  • Strings handle text data and support various operations.
  • Booleans are used for logical operations and control flow.
  • Dates manage date and time information.
  • Arrays store collections of items.
  • Hash Tables store key-value pairs for efficient lookups.
  • Custom Objects represent structured data with multiple properties.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.