Skip to content

Type Accelerators

Type accelerators are short, convenient aliases for .NET types. PowerShell uses .NET under the hood, and every value has a .NET type. Instead of writing long type names like:

[System.Int32]
[System.Collections.Generic.List[string]]
[System.DateTime]

PowerShell provides shorter versions:

[int]
[list[string]]
[datetime]

Type accelerators make scripts easier to read and write, and they allow you to explicitly control the type of a variable, cast values, and create objects.


1. Basic syntax

A type accelerator is written inside square brackets:

[TypeName]

You can use it in three main ways:

  1. Casting a value
  2. Declaring a typed variable
  3. Creating a new object

Each of these is explained below.


2. Casting values

Casting converts a value from one type to another.

2.1 Casting to an integer

[int]$x = "42"

Explanation:

  • "42" is a string.
  • [int] converts it to an integer.
  • $x now holds the numeric value 42.

If the string cannot be converted, PowerShell throws an error:

[int]"hello"   # error

2.2 Casting to a string

[string]$y = 123

123 becomes "123".

2.3 Casting to a boolean

[bool]0     # False
[bool]1     # True
[bool]"hi"  # True (non‑empty strings are True)

2.4 Casting to a datetime

[datetime]"2024-01-01"

This produces a System.DateTime object representing January 1st, 2024.


3. Declaring typed variables

You can force a variable to always hold a specific type:

[int]$count = 10

If you assign an incompatible value later, PowerShell converts it or throws an error:

$count = "20"   # converted to integer 20
$count = "abc"  # error

Typed variables are useful when you want predictable behavior in calculations or comparisons.


4. Creating objects with type accelerators

Many .NET types have accelerators that let you create new objects easily.

4.1 Creating a TimeSpan

$duration = [timespan]::FromMinutes(30)

$duration now holds a 30‑minute time interval.

4.2 Creating a generic list

$list = [System.Collections.Generic.List[int]]::new()
$list.Add(10)
$list.Add(20)

PowerShell also provides a shorter accelerator:

$list = [list[int]]::new()

4.3 Creating a GUID

$id = [guid]::NewGuid()

This produces a unique identifier.


5. Common type accelerators

Here are accelerators students will use frequently:

Accelerator Full .NET Type
[int] System.Int32
[string] System.String
[bool] System.Boolean
[datetime] System.DateTime
[timespan] System.TimeSpan
[guid] System.Guid
[regex] System.Text.RegularExpressions.Regex
[pscustomobject] System.Management.Automation.PSCustomObject
[hashtable] System.Collections.Hashtable
[array] System.Array

These accelerators allow you to work with .NET types without writing long names.


6. Practical examples

6.1 Ensuring numeric math

Without a type accelerator:

$x = "5"
$y = "7"
$x + $y      # "57" (string concatenation)

With a type accelerator:

[int]$x = "5"
[int]$y = "7"
$x + $y      # 12 (integer addition)

6.2 Validating user input

$age = Read-Host "Enter your age"
[int]$age    # converts or errors

If the user enters "abc", PowerShell stops with an error, preventing invalid data from continuing.

6.3 Creating structured objects

$user = [pscustomobject]@{
    Name  = "Alice"
    Age   = 30
    Admin = $true
}

This produces a clean, typed object with named properties.