≡ Menu

C# typeof equivalent in PowerShell

Those who are good with C# knows what typeof does. Basically is used for finding the type data of a given variable or object. How do we do this when it comes to PowerShell? Is there any equivalent keyword for typeof in PowerShell? Let us find out.

PowerShell implements typeof in little different way. Instead of using the typeof keyword like in C#, the PowerShell provides a method called GetType() for each variable or object to find its type. For example, if you want to find data type of a variable called $username, you can know it by calling $username.gettype().

$username = “techibee”


PowerShell Typeof equivalent

You can also check if the returned type is a string, integer or some other data type by comparing it as shown below.

$username = “techibee”

$username.GetType() -eq [string]

$age = 12

$age.GetType() -eq [int32]

$age.GetType() -eq [string]

PowerShell typeof

Do you know any other case where PowerShell’s Gettype() cannot replace typeof keyword in C#? Please post in comments section.

{ 2 comments… add one }
  • Anonymous October 30, 2017, 10:01 pm

    $x=(Get-Acl -Path $Path).GetOwner([System.Security.Principal.NTAccount]) works

    $x=(Get-Acl -Path $Path).GetOwner([System.Security.Principal.NTAccount].GetType()) does not work.

    • Wintel Rocks November 12, 2017, 9:41 pm

      Curious to know what exactly you are trying to achieve… I will be able to comment better if you provide the details.

Leave a Comment