Data Types
Prerequisites: Variables
Every value in PowerShell is a .NET object with a type. PowerShell usually picks the type for you, and converts between types when it makes sense.
(42).GetType().Name
(3.14).GetType().Name
('hello').GetType().Name
($true).GetType().Name
(Get-Date -Year 2025).GetType().Name
(1, 2, 3).GetType().Name
Int32
Double
String
Boolean
DateTime
Object[]
Type literals and casting
Square brackets name a type. Put one before a value to convert it:
[int]'42' + 1
[double]'3.5' * 2
[string]42 + 1
[datetime]'2025-04-22'
[bool]0
[int]3.7
43
7
421
Tuesday, April 22, 2025 12:00:00 AM
False
4
Notice [int]3.7 is 4: converting to an integer rounds (to the
nearest even number on a tie, like 2.5 → 2), unlike many languages that
truncate.
Typed variables
Put a type before a variable to lock it to that type:
[int]$count = 5
$count = '10'
$count + 1
try { $count = 'ten' } catch { "Error: $($_.Exception.Message)" }
11
Error: Cannot convert value "ten" to type "System.Int32". Error: "The input string 'ten' was not in a correct format."
The string '10' was converted to a number; 'ten' couldn't be, so the
assignment failed. Typed variables and parameters catch mistakes early.
Conversion follows the left side
'5' + 3
5 + '3'
'5' * 3
53
8
555
In an expression, PowerShell converts the right-hand value to the type of the left one: text plus a number joins text, a number plus text adds numbers, and text times a number repeats the text. When in doubt, convert explicitly.