Determine the Type of a PowerShell Argument
Introduction
You can get the type of a variable in PowerShell with the method GetType()
Example
$myVar = 42 $myVar.GetType()
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32 System.ValueType
Data Types
Let us go through different data types
$var_int = 0 $var_long = 2147483648 $var_float = 1.0 $var_str = "testsetup.ru" $var_bool = $true $var_arr = 1, 2, 3, 4 $var_hash = @{ url = "heihei.ru" topic = "travel" } Write-Output $var_int.GetType() Write-Output $var_long.GetType() Write-Output $var_float.GetType() Write-Output $var_str.GetType() Write-Output $var_bool.GetType() Write-Output $var_arr.GetType() Write-Output $var_hash.GetType()
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32 System.ValueType True True Int64 System.ValueType True True Double System.ValueType True True String System.Object True True Boolean System.ValueType True True Object[] System.Array True True Hashtable System.Object
Let us look at more exotic data types.
The GetType() method returns an object of the special class System.Reflection.TypeInfo
$var_int = 0 $get_t = $var_int.GetType() Write-Output $get_t.GetType()
IsPublic IsSerial Name BaseType -------- -------- ---- -------- False True RuntimeType System.Reflection.TypeInfo
Let us look at getting data from the command Get-Location
$var_pwd = Invoke-Command -ScriptBlock { Get-Location } Write-Output $var_pwd.GetType()
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False PathInfo System.Object
If you output the contents of $var_pwd now, it will be the same as the output of the command Get-Location.
For brevity let us drop Invoke-Command.
$var_pwd = Get-Location Write-Output $var_pwd
Drive : C Provider : Microsoft.PowerShell.Core\FileSystem ProviderPath : C:\sandbox Path : C:\sandbox
If we now convert $var_pwd to a string, the result will be very different
Write-Output $var_pwd.ToString()
C:\sandbox
To get the drive name out of the PathInfo object you need to run the following command.
Write-Output $var_pwd.Drive.Name
C
The GetType() method cannot be applied to an object of type $null
$var_null = $null Write-Output $var_null.GetType()
You cannot call a method on a null-valued expression. At C:\get_type_demo.ps1:9 char:1 + Write-Output $var_null.GetType() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
In Other Languages
- C : there is no such function.
-
C++
:
a similar task is solved by the function
typeid()
Read the article: «How to Determine the Type of a C++ Variable»
- C# : there is a similar function GetType()
- Go : the function typeof() is available from the reflect library
- Java : the operator instanceof
-
PHP
:
this task is solved by
gettype()
Read the article: «How to Determine the Type of a PHP Variable»
-
Python
:
a similar function
type()
and a slightly different function
isinstance()
with which you can solve the same task.
Read the article: «How to Determine the Type of a Python Variable»
Article author: Andrei Olegovich