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):
Null values and null checking:
Custom objects (structured output):
Type checking and conversion:
Nullable value types (.NET Nullable):
Check members and explore types:
Recommendations:
- Use
[decimal]for precise decimal calculations to avoid binary floating-point errors. - For objects returned from file/network operations, use
Get-Memberto observe before processing. - For large-scale data processing, prefer strongly-typed collections (List/Dictionary) for better performance.