Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Get-Help tell you the type returned by a cmdlet?

Tags:

powershell

Is there a way to get the return type for a PowerShell cmdlet from the Get-Help command? I prefer to get this information without actually invoking the command and use GetType() on it. Seems like the object return type should be in the documentation somewhere?

Example: I type Get-Help Get-Content. Where in the documentation does it tell me it returns a String Object? or String[] object?

Is there some flag I need to provide to Get-Help to get this information?

like image 463
Bimo Avatar asked Jan 21 '26 15:01

Bimo


2 Answers

Get-Command can also be helpful in this scenario:

PS C:\> Get-Command Get-Content |Select OutputType

OutputType
----------
{System.Byte, System.String}
like image 67
Mathias R. Jessen Avatar answered Jan 23 '26 13:01

Mathias R. Jessen


If you run Get-Help -Name Get-Content -Full, you'll see all the help members. For your question, there is an OUTPUTS field:

OUTPUTS  
    System.Object, System.String  
        Get-Content returns objects that represent the content that it gets.
        The object type depends on the content type. If you use the Stream
        parameter, the cmdlet returns the alternate data stream contents as
        a string.

An array isn't really a type all on its own, but you can force anything to return an array to you by wrapping it in an array literal:

File.txt

This is one line of text

Command:

@(Get-Content -Path File.txt)

Now you can access the .Count and other array type members and when you use -match (for example), it will return the full string if it is successful instead of $True/$False

like image 40
Maximilian Burszley Avatar answered Jan 23 '26 11:01

Maximilian Burszley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!