Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell equivalent of the C# "is" operator?

Tags:

powershell

In a PowerShell script, I need to determine whether a .NET method call is actually returning the correct type of object, or at least a compatible type. How can I do this?

like image 802
Jay Sullivan Avatar asked Feb 17 '12 15:02

Jay Sullivan


People also ask

What is control C in PowerShell?

Stop Execution CTRL + C can be used when the context is unambiguous (when there is no text selected). Tab (to next script) CTRL + TAB Note: Tab to next script works only when you have a single Windows PowerShell tab open, or when you have more than one Windows PowerShell tab open, but the focus is in the Script Pane.

What does $_ mean in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

What does @() mean in PowerShell?

What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.


2 Answers

Something like this:

$a -is [string]

Or you can use GetType() and see whether the type is what you want.

like image 127
manojlds Avatar answered Oct 07 '22 08:10

manojlds


$yourObject.pstypenames will return the complete inheritance chain.

like image 39
David Brabant Avatar answered Oct 07 '22 07:10

David Brabant