How to Get the Type of a Variable in PowerShell?

Recently, someone asked me how to get the type of a PowerShell variable in a PowerShell user group in the USA. I suggested a few methods, and in this tutorial, I will show you how to get the type of a variable in PowerShell.

To get the type of a variable in PowerShell, use the GetType() method. For example, if you have a variable $usaCity = "New York", you can retrieve its type by appending .GetType().Name to the variable, like this: $usaCity.GetType().Name. This will return “String”, indicating that the variable is of type System.String.

PowerShell is a dynamically typed language, meaning that the variable type is determined at runtime based on the value assigned to it.

The GetType() Method

In PowerShell, the primary way to get the type of a variable is by using the GetType() method. This method is available on every object in PowerShell and returns a System.Type object that provides information about the type of the variable.

Let’s take a look at a real-world example:

$usaCity = "New York"
$usaCity.GetType().Name

In this example, we assign the string value “New York” to the variable $usaCity. By appending .GetType().Name to the variable, PowerShell returns the type of the variable, which is “String” in this case.

You can see the output in the screenshot below, after I executed the PowerShell script using VS code.

powershell get variable type

It’s important to note that when using GetType(), you need to include parentheses to call the method properly.

Check out Check if a Variable is Null in PowerShell

Dynamic Type Assignment

One of the neat features of PowerShell is its ability to assign types to variables based on the assigned value dynamically. For instance:

$usaPopulation = 331002651 # System.Int32
$usaCapital = "Washington, D.C." # System.String
$usaStates = 50, "fifty" # System.Object[]

In this example, PowerShell automatically assigns the appropriate types to the variables based on their values. $usaPopulation is an integer (System.Int32), $usaCapital is a string (System.String), and $usaStates is an array of objects (System.Object[]).

Using Get-Member for Detailed Type Information

While GetType() in PowerShell provides the basic type information, you can use the Get-Member cmdlet to obtain more detailed information about a variable’s object type. For example:

$usaPresident = "Who ever it is"
$usaPresident | Get-Member

This code will output a list of members (properties and methods) available for the $usaPresident variable. So, this is another way to get the type of variable.

Here is the output in the screenshot below:

powershell get type of variable

Check out PowerShell Variable Naming Conventions

PowerShell Check Variable Type Examples

Now, let me show you a few other examples to help you learn more about checking variable types in PowerShell.

Example 1: Basic Types

Let’s start with some basic examples. Suppose we have a variable representing the population of New York City. Then you can write the PowerShell script below to get the variable type.

$populationNYC = 8419000
$populationType = $populationNYC.GetType()
Write-Output "The type of populationNYC is: $($populationType.FullName)"

In this example, the output will be:

The type of populationNYC is: System.Int32

Here, the GetType() method is used to determine that the variable $populationNYC is of type System.Int32.

Here is the output in the screenshot below for your reference.

powershell check variable type

Example 2: String Type

Now, let’s consider a variable that holds the name of a famous landmark in the USA:

$landmark = "Statue of Liberty"
$landmarkType = $landmark.GetType()
Write-Output "The type of landmark is: $($landmarkType.FullName)"

The output will be:

The type of landmark is: System.String

This shows that the variable $landmark is of type System.String.

Example 3: Array Type

PowerShell also supports arrays. Let’s create an array of famous American cities:

$cities = @("New York", "Los Angeles", "Chicago", "Houston", "Phoenix")
$citiesType = $cities.GetType()
Write-Output "The type of cities is: $($citiesType.FullName)"

The output will be:

The type of cities is: System.Object[]

This indicates that $cities is an array of objects (System.Object[]).

Example 4: Function Parameter Validation

Suppose you have a function that calculates the area of a rectangle, and you want to ensure the parameters are integers:

function Get-Area {
    param (
        [int]$length,
        [int]$width
    )
    return $length * $width
}

$length = 10
$width = 20
Write-Output "The area of the rectangle is: $(Get-Area -length $length -width $width)"

By specifying [int] before the parameters, you ensure that only integer values are accepted, preventing potential runtime errors.

Conclusion

In this tutorial, I have explained how to get the type of variable in PowerShell using different methods with examples. I always recommended to use the GetType() method and Get-Member cmdlet to easily determine the type of a variable and access its properties and methods.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.