PowerShell Data Types Explained with Examples

PowerShell is a dynamically typed language, which means you rarely have to declare a type explicitly. But that convenience hides real bugs: add a string to a number and PowerShell does something you probably didn’t expect. Understanding PowerShell data types prevents those surprises, helps you write more efficient code, and lets you catch type errors before they become production incidents. This guide covers every common type with concrete examples.
String, Int, Double, and Bool
These four types handle the vast majority of everyday scripting:
# String — sequence of characters
[string]$path = 'C:\Logs\app.log'
# Int — 32-bit integer
[int]$port = 443
# Double — floating point
[double]$ratio = 0.95
# Bool — true or false
[bool]$isEnabled = $true
# Check the type of any variable
$path.GetType().FullName
$port.GetType().FullName
System.String
System.Int32
The bracket notation ([string], [int]) is optional but recommended when the type matters. Without it, PowerShell infers the type from the assigned value: $x = 5 creates an Int32, $x = '5' creates a String.
DateTime and TimeSpan Types
Date and time handling is built into PowerShell via .NET’s DateTime and TimeSpan:
# Get current date
$now = Get-Date
# Create a specific date
$deadline = [DateTime]'2026-12-31'
# Calculate difference
$daysLeft = ($deadline - $now).Days
Write-Output "Days until deadline: $daysLeft"
# Add time with TimeSpan
$future = $now.AddDays(30)
$future.ToString('yyyy-MM-dd')
# Parse a date string
$logDate = [DateTime]::ParseExact('2026-05-04', 'yyyy-MM-dd', $null)
Days until deadline: 241
2026-06-03
DateTime arithmetic uses the TimeSpan type automatically. Subtracting two DateTime values gives you a TimeSpan with properties like .Days, .Hours, and .TotalSeconds.
Arrays and ArrayList
A standard PowerShell array is a fixed-size collection created with the comma operator or @():
# Create a typed array
$servers = @('web01', 'web02', 'db01')
# Access by index
$servers[0] # web01
# Check the type
$servers.GetType().Name # Object[]
# ArrayList — use when you need to add/remove items frequently
$list = [System.Collections.ArrayList]@()
$list.Add('web01') | Out-Null
$list.Add('web02') | Out-Null
$list.Count
web01
Object[]
2
The key difference: adding to a regular array ($arr += 'item') creates a new array every time — expensive for thousands of items. Use ArrayList or a .NET List[string] when you’re building collections in a loop.
Hashtables and Ordered Dictionaries
Hashtables store key-value pairs. They’re perfect for configuration data and lookup tables:
# Create a hashtable
$config = @{
Server = 'db01'
Port = 1433
Database = 'Production'
}
# Access a value
$config['Server']
$config.Port
# Ordered hashtable preserves insertion order
$ordered = [ordered]@{
First = 1
Second = 2
Third = 3
}
db01
1433
How to Check a Variable’s Type
Three reliable ways to inspect a variable’s type:
$value = 42
# Method 1: GetType()
$value.GetType()
# Method 2: GetType().FullName for the .NET type name
$value.GetType().FullName
# Method 3: -is operator (returns boolean)
$value -is [int]
$value -is [string]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
System.Int32
True
False
Explicit Type Casting
Cast values explicitly to prevent type coercion surprises:
# String to int
[int]'42' # 42
# Int to string
[string]42 # '42'
# String to DateTime
[DateTime]'2026-01-15'
# Safe cast — returns $null instead of throwing on failure
$parsed = '99abc' -as [int]
$parsed # $null (conversion failed silently)
The -as operator is safer than direct casting in [] brackets: if conversion fails, -as returns $null rather than throwing a terminating error. Use -as when the input is user-provided or otherwise uncertain.
Common Errors and Fixes
-
String arithmetic returning concatenation not sum:
'5' + 3returns53because the left operand is a string and+becomes concatenation. Fix: cast to int first —[int]'5' + 3returns8. This often appears when reading CSV data where numbers come in as strings. -
Type casting fails with invalid format:
[int]'abc'throwsCannot convert value "abc" to type "System.Int32". Use'abc' -as [int]instead, which returns$nullon failure, and then check withif ($null -ne $result).
Related Cmdlets / See Also
Wrapping Up
PowerShell’s type system is flexible but not invisible — string arithmetic and implicit conversions can bite you. Use GetType() and the -is operator to inspect values, cast explicitly with [] when type matters, and prefer -as for safe conversions from user input. Your next step: dig into arrays and hashtables for working with collections of data.


