If you are starting PowerShell and want to become an expert in it, you should have a complete understanding of PowerShell data types. In this PowerShell tutorial, I will show you various data types in PowerShell, including integers, floats, doubles, decimals, characters, strings, Booleans, dates, arrays, hash tables, and custom objects.
I will also show you how to check the data type in PowerShell.
Various Data Types in PowerShell
Here are a few data types in PowerShell with examples.
1. Integer
Integers are whole numbers without any fractional component. They can be positive or negative. In PowerShell, integers are typically of the type [int], which corresponds to a 32-bit signed integer in the .NET framework.
Syntax
[int]$integerVariable = 10Examples
Here is a complete example.
[int]$positiveInteger = 42
[int]$negativeInteger = -42
# Arithmetic operations
$sum = $positiveInteger + $negativeInteger
Write-Output $sumYou can see the output in the screenshot below:

2. Float and Double
Floats and doubles represent numbers with fractional components. Floats (single precision) and doubles (double precision) differ in the amount of precision and range they offer. Floats are generally used when less precision is acceptable, while doubles are used for more accurate calculations.
Syntax
Here is the syntax for float and double data types in PowerShell.
[float]$floatVariable = 10.5
[double]$doubleVariable = 10.5Examples
Here is an example.
[float]$floatNumber = 3.14
[double]$doubleNumber = 3.141592653589793
# Arithmetic operations
$floatSum = $floatNumber + 2.86
$doubleSum = $doubleNumber + 2.86
Write-Output $floatSum # Output: 6.00000010490417
Write-Output $doubleSum # Output: 6.001592653589793When I executed the script using VS code and the output, you can see the output in the screenshot below:

3. Decimal
Decimals are used for high-precision arithmetic, especially useful in financial calculations where rounding errors can be unacceptable. The [decimal] type in PowerShell provides a higher precision and a smaller range of values compared to floats and doubles, making it ideal for currency and other precise calculations.
Syntax
Here is the syntax for decimal data types in PowerShell.
[decimal]$decimalVariable = 10.5Examples
Here is an example.
[decimal]$decimalNumber = 123.4567890123456789012345678
# Arithmetic operations
$decimalSum = $decimalNumber + 0.5432109876543210987654321
Write-Output $decimalSum Read How to Handle Errors with Try-Catch in PowerShell?
4. Character
A character data type in PowerShell represents a single character, such as a letter, digit, or symbol. In PowerShell, characters are of the type [char], which corresponds to a Unicode character. This type is useful for handling individual characters in strings, performing character-level operations, and working with text data.
Syntax
Here is the syntax of character data type in PowerShell.
[char]$charVariable = 'A'Examples
[char]$charA = 'A'
[char]$charB = 'B'
# Concatenation
$charConcat = "$charA$charB"
Write-Output $charConcatYou can see the output in the screenshot below:

5. String
Strings are sequences of characters used to represent text. In PowerShell, strings are of the type [string]. Strings support a wide range of operations, including concatenation, substring extraction, and pattern matching.
Syntax
Here is the syntax:
[string]$stringVariable = "Hello, World!"Examples
Here is an example.
[string]$greeting = "Hello"
[string]$name = "World"
# String concatenation
$fullGreeting = "$greeting, $name!"
Write-Output $fullGreeting # Output: Hello, World!
# String length
$stringLength = $fullGreeting.Length
Write-Output $stringLength # Output: 136. Booleans
Booleans represent true or false values for logical operations and control flow. In PowerShell, Booleans are of the type [bool]. They are essential for making decisions in scripts, such as conditional statements and loops, which allow scripts to execute different code paths based on certain conditions.
Syntax
Here is the syntax for booleans of data types in PowerShell.
[bool]$booleanVariable = $trueExamples
Here is a simple example of how to use boolean data types in PowerShell.
[bool]$isTrue = $true
[bool]$isFalse = $false
# Conditional check
if ($isTrue) {
Write-Output "This is true."
} else {
Write-Output "This is false."
}I executed the above PowerShell script, and you can see the output in the screenshot below:

7. Dates
Dates represent date and time values. In PowerShell, dates are of the type [datetime], which provides various methods and properties for manipulating date and time data.
Syntax
Here is the syntax
[datetime]$dateVariable = Get-DateExamples
Here is an example.
[datetime]$currentDate = Get-Date
Write-Output $currentDate # Output: Current date and time
# Specific date
[datetime]$specificDate = [datetime]"2024-01-01"
Write-Output $specificDate # Output: 01 January 2024 00:00:008. Arrays
Arrays are collections of items of the same or different types stored in a single variable. In PowerShell, arrays are of the type [array]. They are useful for storing lists of data, such as numbers, strings, or objects. They also provide various methods for accessing, adding, and manipulating elements.
Syntax
Here is the syntax:
[array]$arrayVariable = @(1, 2, 3, 4, 5)Examples
Here is a simple example of how to use the array data type in PowerShell.
[array]$numbers = @(1, 2, 3, 4, 5)
Write-Output $numbers # Output: 1 2 3 4 5
# Accessing elements
$firstNumber = $numbers[0]
Write-Output $firstNumber # Output: 1
# Adding elements
$numbers += 6
Write-Output $numbers # Output: 1 2 3 4 5 69. Hash Tables
Hash tables are collections of key-value pairs, similar to dictionaries in other programming languages. In PowerShell, hash tables are of the type [hashtable]. Hash tables store data that can be quickly accessed via a unique key. They are useful for configuring settings, mapping data, and storing structured information.
Syntax
Here is the syntax:
$hashTable = @{ Key1 = 'Value1'; Key2 = 'Value2' }Examples
Here is a complete example of how to use the hashtable data type in PowerShell.
$person = @{
Name = "John"
Age = 30
Occupation = "Developer"
}
# Accessing values
$name = $person["Name"]
Write-Output $name # Output: John
# Adding key-value pairs
$person["Country"] = "USA"
Write-Output $personYou can see the output in the screenshot below:

10. Custom Objects
Custom objects allow you to create structured data by defining properties. In PowerShell, you can create custom objects using the New-Object cmdlet or by using a more modern and concise syntax with [PSCustomObject].
Syntax
Here is the syntax of custom objects data type in PowerShell.
$customObject = New-Object PSObject -Property @{
Property1 = 'Value1'
Property2 = 'Value2'
}Examples
Here is a simple example to understand about the PowerShell custom objects data type.
$person = New-Object PSObject -Property @{
Name = "Jane"
Age = 28
Occupation = "Designer"
}
# Accessing properties
Write-Output $person.Name # Output: Jane
# Adding properties
$person.Country = "Canada"
Write-Output $person How to Check Data Type in PowerShell?
Now, let me show you how to check data type in PowerShell with a few examples.
In PowerShell, you can determine the data type of a variable by using the GetType() method. This method provides detailed information about the variable type, such as its name and base type. Here’s how you can use it:
- Assign a value to a variable:
$x = 10 - Use the
GetType()method to find out the type:$x.GetType()
Here are a few examples to understand this better.
Example 1: Integer Type
The below PowerShell script is to know about the integer type in PowerShell.
$x = 10
$xType = $x.GetType()
Write-Output $xTypeOutput:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueTypeIn this example, $x is of type Int32, which is an integer.
Example 2: String Type
Here is an example for a string data type in PowerShell.
$str = "Hello, PowerShell!"
$strType = $str.GetType()
Write-Output $strTypeOutput:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Here, $str is of type String.
Example 3: Array Type
Here is an example for a PowerShell array data type.
$arr = @(1, 2, 3)
$arrType = $arr.GetType()
Write-Output $arrTypeOutput:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
In this case, $arr is an array of objects.
Conclusion
I hope you now have an idea of the various data types in PowerShell. I have shown you the syntax and examples of each data type. Here is a summary:
- Integers are used for whole number arithmetic.
- Floats and Doubles handle fractional numbers with varying precision.
- Decimals are crucial for high-precision arithmetic, especially in financial contexts.
- Characters represent single Unicode characters.
- Strings handle text data and support various operations.
- Booleans are used for logical operations and control flow.
- Dates manage date and time information.
- Arrays store collections of items.
- Hash Tables store key-value pairs for efficient lookups.
- Custom Objects represent structured data with multiple properties.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.