Skip to content

Data Types

PowerShell is based on .NET, where objects are first-class citizens. Common scalar types:

  • Numeric: [int] [long] [double] [decimal] [byte]
  • Text: [string] (immutable)
  • Date/Time: [datetime] [timespan]
  • Boolean: [bool] ($true/$false)
  • Others: Script block [scriptblock], regex [regex], XML [xml]

Collection types:

  • Arrays: [object[]] or strongly typed like [int[]]
  • Hash tables: [hashtable], ordered hash table [ordered]
  • Lists/Dictionaries (high performance): [System.Collections.Generic.List[T]], [System.Collections.Generic.Dictionary[K,V]]

Type accelerators (common aliases) examples: [int] [string] [datetime] [timespan] [pscustomobject] [hashtable] [ordered] [regex] [xml]

Type conversion (casting and parsing):

powershell
[int]'42'                 # 42
[double]'3.14'            # 3.14 (note decimal point and locale settings)
[decimal]'19.99'
[datetime]'2025-01-01'
[timespan]'1:30:00'       # 1 hour 30 minutes
# Parse API (controllable culture/format):
[datetime]::ParseExact('2025-01-01','yyyy-MM-dd',$null)

Null values and null checking:

powershell
$null -eq $x   # Recommended approach, avoids calling .Equals() when $x is $null
[string]::IsNullOrEmpty($s)
[string]::IsNullOrWhiteSpace($s)

Custom objects (structured output):

powershell
[pscustomobject]@{
  Name='Alice'; Age=30; Joined=[datetime]'2024-05-01'
}

Type checking and conversion:

powershell
1 -is [int]           # True
'123' -as [int]       # 123; returns $null on failure instead of error
'abc' -as [int]       # $null

Nullable value types (.NET Nullable):

powershell
[Nullable[int]]$n = $null   # or [int]?$n in PowerShell 7+
$n = 5

Check members and explore types:

powershell
Get-Date | Get-Member    # View type and available properties/methods

Recommendations:

  • Use [decimal] for precise decimal calculations to avoid binary floating-point errors.
  • For objects returned from file/network operations, use Get-Member to observe before processing.
  • For large-scale data processing, prefer strongly-typed collections (List/Dictionary) for better performance.

Content is for learning and research only.