4

In PowerShell, if a command returns an array of one or more objects, I can find out the class of the object by the following method:

$ab = SampleCommand
$ab[0].getType()

But, how can I find out the default return type of a command if the command returned nothing (array of 0 values)?

Note: For example, I am working on SCOM PowerShell commands, and I am trying to find the default return class type of command get-scomscadvisoragent, but it returns nothing as the advisor agent is not configured in my lab setup. Hence, I am not able to get the class of returned object.

3
  • 1
    The help information doesn't specify what it returns? Commented Jun 14, 2018 at 17:16
  • 2
    If something returns nothing, the return type is $null/[System.Void]. Commented Jun 14, 2018 at 17:21
  • Generically speaking, gettype().Name and gettype().DeclaredFields are also two very useful properties of that method. Commented Dec 28, 2022 at 13:07

1 Answer 1

11

If you want to determine the type of objects a given command outputs in principle, use (Get-Command <cmd>).OutputType; in your case:

(Get-Command get-scomscadvisoragent).OutputType

Note, however, that this only works if the target command has explicitly declared its output types via one or more OutputType attributes.


Cmdlets / advanced functions may alternatively / additionally describe their output types in a less formal way, via the .OUTPUTS section of their help text (in comment-based help).
Therefore, if the above didn't work, you can also try:

(Get-Help get-scomscadvisoragent).returnvalues

The latter information can also be found in the OUTPUTS section of the output from
Get-Help -Full <cmd>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.