TExcelTAD
Home › PowerShell › PowerShell Data Types

PowerShell Data Types

PowerShell is built on .NET, so every value has a .NET type. Most of the time PowerShell picks a suitable type for you, but knowing the common types and how to convert between them prevents many bugs.

powershell · try it yourself
(42).GetType().Name
(3.14).GetType().Name
('text').GetType().Name
($true).GetType().Name
(1, 2, 3).GetType().Name
(@{ a = 1 }).GetType().Name
(Get-Date -Date '2026-01-01').GetType().Name
Int32
Double
String
Boolean
Object[]
Hashtable
DateTime

Common types and their accelerators

PowerShell has short names called type accelerators for frequently used types. Write them in square brackets:

Accelerator .NET type Example value
[int] System.Int32 42
[long] System.Int64 5000000000
[double] System.Double 3.14
[decimal] System.Decimal 19.99d (exact money maths)
[string] System.String 'hello'
[char] System.Char [char]'A'
[bool] System.Boolean $true, $false
[datetime] System.DateTime [datetime]'2026-01-01'
[array], [object[]] System.Object[] 1, 2, 3
[hashtable] System.Collections.Hashtable @{ a = 1 }
[version] System.Version [version]'7.4.2'
[regex], [xml], [ipaddress], [guid] specialised parsers

Casting: converting a value

Put a type in front of a value to convert (cast) it:

powershell · try it yourself
[int]'42' + 8
[double]'2.5' * 2
[int]2.5
[int]3.5
[string]123 + 4
[bool]0
[bool]'false'
[char]65
[int][char]'a'
50
5
2
4
1234
False
True
A
97

Two surprises to remember:

  • [int] rounds to the nearest even number at .5 ("banker's rounding"): [int]2.5 is 2 but [int]3.5 is 4. Use [math]::Floor() or [math]::Truncate() to cut off the decimals.
  • [bool]'false' is True: any non-empty string is true. Only '', 0, $null and empty arrays are false.

Automatic conversion: the left side decides

When you combine values of different types, PowerShell converts the right operand to the type of the left one:

powershell · try it yourself
'5' + 3
5 + '3'
'5' * 3
5 * '3'
'10' -eq 10
10 -eq '10.0'
53
8
555
15
True
True

'5' + 3 is string concatenation (53), while 5 + '3' is addition (8). Multiplying a string repeats it. Keep this rule in mind when values come from files or user input: they are strings until you convert them.

Typed variables

Putting a type in front of the variable at assignment locks the variable to that type. Every later assignment is converted, or fails:

powershell · try it yourself
[int]$count = '15'
$count += 1
$count
$count.GetType().Name

try {
    $count = 'many'
} catch {
    "Error: $($_.Exception.Message)"
}
16
Int32
Error: Cannot convert value "many" to type "System.Int32". Error: "The input string 'many' was not in a correct format."

Checking types: -is and -as

-is tests a type; -as converts if possible and returns $null instead of throwing an error when it can't:

powershell · try it yourself
42 -is [int]
'42' -is [int]
'42' -as [int]
$null -eq ('forty-two' -as [int])
[datetime]'2026-07-04' -is [datetime]
True
False
42
True
True

Tip: Use -as when you want to try a conversion, for example on user input, and check the result for $null afterwards.

Test yourself

What is '7' + 3?

  1. 10
  2. 73
  3. An error
  4. '7 3'

What is [int]2.5?

  1. 2
  2. 3
  3. 2.5
  4. An error

What does 'abc' -as [int] return?

  1. 0
  2. An error
  3. $null
  4. 'abc'