PowerShell Data Types
A data type tells PowerShell what kind of value a variable holds — text, a number, a true/false flag, or something more complex. Knowing data types prevents calculation errors, type mismatches, and unexpected output in scripts.
How PowerShell Handles Data Types
PowerShell uses the .NET type system. Every value in PowerShell is an object, and every object has a type. PowerShell detects the type automatically when a value is assigned — this is called dynamic typing. The type can also be locked in manually — this is called static typing.
Value Assigned Type Detected ------------------ ----------------- "Hello" --> [string] 42 --> [int] 3.14 --> [double] $true --> [bool] Get-Date --> [DateTime] @(1,2,3) --> [array]
Core Data Types in PowerShell
| Type Name | .NET Type | Description | Example |
|---|---|---|---|
| [string] | System.String | Text characters | "PowerShell" |
| [int] | System.Int32 | Whole numbers (±2 billion) | 42 |
| [long] | System.Int64 | Very large whole numbers | 9876543210 |
| [double] | System.Double | Decimal numbers (high range) | 3.14159 |
| [float] | System.Single | Decimal numbers (lower precision) | 3.14f |
| [decimal] | System.Decimal | High-precision decimal (financial) | 99999.99 |
| [bool] | System.Boolean | True or False | $true / $false |
| [char] | System.Char | Single character | 'A' |
| [datetime] | System.DateTime | Date and time | Get-Date |
| [array] | System.Array | Collection of values | @(1, 2, 3) |
| [hashtable] | System.Collections.Hashtable | Key-value pairs | @{Name="Ali"} |
| [object] | System.Object | Generic object (any type) | (any value) |
| [void] | System.Void | No return value | (used in functions) |
String Data Type
Strings hold text. They are enclosed in double quotes or single quotes.
[string]$firstName = "Arjun"
[string]$lastName = "Sharma"
# Concatenate strings
$fullName = $firstName + " " + $lastName
Write-Host $fullName
# String length
Write-Host $fullName.Length
# Convert to uppercase
Write-Host $fullName.ToUpper()
# Convert to lowercase
Write-Host $fullName.ToLower()
Output:
Arjun Sharma
12
ARJUN SHARMA
arjun sharma
Integer Data Type
Integers hold whole numbers. They do not have a decimal point.
[int]$apples = 10
[int]$oranges = 4
$total = $apples + $oranges
$diff = $apples - $oranges
$mult = $apples * $oranges
$div = $apples / $oranges
Write-Host "Total: $total"
Write-Host "Diff: $diff"
Write-Host "Multiply: $mult"
Write-Host "Divide: $div"
Output:
Total: 14
Diff: 6
Multiply: 40
Divide: 2.5
Double and Decimal Data Types
[double]$pi = 3.14159265
[decimal]$invoice = 1499.99
Write-Host "Pi value: $pi"
Write-Host "Invoice amount: $invoice"
# Decimal is more precise – use for money calculations
[decimal]$tax = 0.18
[decimal]$total = $invoice + ($invoice * $tax)
Write-Host "Total with tax: $total"
Output:
Pi value: 3.14159265
Invoice amount: 1499.99
Total with tax: 1769.9882
Boolean Data Type
A boolean holds only two values: $true or $false. Booleans control decision-making in scripts.
[bool]$isLoggedIn = $true
[bool]$hasError = $false
Write-Host "User logged in: $isLoggedIn"
Write-Host "Error present: $hasError"
# Use in an if statement
if ($isLoggedIn) {
Write-Host "Access granted"
}
Output:
User logged in: True
Error present: False
Access granted
DateTime Data Type
[datetime]$today = Get-Date
[datetime]$birthday = "1995-08-15"
Write-Host "Today: $today"
Write-Host "Birthday: $birthday"
# Calculate age
$age = ($today - $birthday).Days / 365
Write-Host "Age in years: $([math]::Round($age, 1))"
Output:
Today: 03/21/2026 10:30:00
Birthday: 08/15/1995 00:00:00
Age in years: 30.6
Type Conversion
PowerShell can convert one data type into another. This is called type casting.
Implicit Conversion (Automatic)
$num = 10
$result = $num + 5.5 # int + double → PowerShell returns double automatically
Write-Host $result # Output: 15.5
Explicit Conversion (Manual)
# Convert string to integer
$strNum = "100"
$intNum = [int]$strNum
Write-Host ($intNum + 50) # Output: 150
# Convert integer to string
$num = 42
$strVal = [string]$num
Write-Host ("Number is: " + $strVal) # Output: Number is: 42
# Convert string to boolean
[bool]"true" # Returns True
[bool]"false" # Returns True (non-empty string is always True!)
[bool]"" # Returns False (empty string is False)
# Convert string to DateTime
[datetime]"2026-01-01"
Checking Data Type
$value = 3.14
# Method 1 – GetType()
$value.GetType().Name # Output: Double
# Method 2 – is operator
$value -is [double] # Output: True
$value -is [string] # Output: False
# Method 3 – GetType().FullName
$value.GetType().FullName # Output: System.Double
Type Mismatch Example
# What happens when types do not match
$a = "10" # String
$b = 5 # Integer
Write-Host ($a + $b) # Output: 105 (string concatenation, not addition!)
Write-Host ([int]$a + $b) # Output: 15 (correct – cast string to int first)
This example shows why type awareness matters. Adding a string "10" to an integer 5 returns "105" (string concat), not 15. Explicitly converting $a to [int] fixes the result.
Null Values
$emptyValue = $null
# Check if a variable is null
if ($emptyValue -eq $null) {
Write-Host "Variable has no value"
}
# Assign a default if null
$name = $null
$displayName = if ($name) { $name } else { "Guest" }
Write-Host "Hello, $displayName"
Output:
Variable has no value
Hello, Guest
Summary
PowerShell supports all standard data types through the .NET type system. Strings store text, integers and doubles hold numbers, booleans store true/false values, and DateTime handles date operations. PowerShell detects types automatically but explicit type declarations prevent hard-to-find bugs, especially in calculations involving mixed types. Always use [decimal] for financial values and [datetime] when working with dates and durations.
