Nobodys perfect

While PowerShell is by far the best 1.0 release I have worked with in a really long time, it isn’t without its faults.  Today I was trying to figure out if it was possible to programmatically get the type(s) of the object(s) that will be returned from a cmdlet without running that cmdlet.  So far I haven’t been successful, but while exploring I came across a big inconsistency in the current embedded documentation for PowerShell cmdlets.

Take at look at this script:

Get-Command -commandType cmdlet `
    | Add-Member -passThru -name returnType -memberType ScriptProperty -value `
        { (Get-Help $this -full).ReturnValues.ReturnValue.Type.Name } `
    | Add-Member -passThru -name returnTypeDescription -memberType ScriptProperty -value `
        { `
            $description = (Get-Help $this -full).ReturnValues.ReturnValue.Type.Description | out-string; `
            if ($description -ne $null) { $description.Replace(“`r`n”,“”) } `
        } `
    | Format-Table -property name,returnType,returnTypeDescription

This script will get all cmdlets, add two properties to those cmdlets (the return type and the return type description, as extracted from the help documentation), and then output a table with the cmdlet name, the return type and the return type description.  The results, well, they leave a lot to be desired. 🙂

Since the get-help cmdlet only shows the return type and not the return type description in the help documentation it generates, usually the information about the return type is either partially missing or completely missing.

Hopefully this will get cleaned up in a future release of PowerShell.

Oh, and if someone out there knows how to programmatically determine the return type of a cmdlet without calling it and wants to share that knowledge through comments, that would be swell. 🙂

Kirk out.

Technorati Tags: , ,

3 thoughts on “Nobodys perfect

  1. Hi Kirk,

    It’s a bit hard to find out the types of the objects returned from a cmdlet, since the ProcessRecord method has a void return type, and the cmdlet can return any sequence of objects to the output stream using WriteObject — so even if you looked inside the code of a cmdlet, you’d see different objects returned (such as directoryinfo’s and fileinfo’s from the get-childitem cmdlet).

    So, I think looking at the documentation may be the only way to go 😦

    Good work creating a script to pull the info out of the help. I guess you could add members to the System.Management.Automation.CmdletInfo type within your profile and perhaps even change the output format? (update-typedata/update-formatdata)

    Cheers,

    Kirk
    [A different Kirk]

  2. I don’t think this is possible and I would indeed like to be able to get this info myself. Could make PowerGUI script editor much nicer, etc. Another similar task is knowing whether output of one cmdlet can be piped into another.

  3. Hi other Kirk (Jackson),

    I agree that finding the return type would be hard using reflection. That’s why I went digging in the embedded help documentation to get it. Too bad it isn’t there in this release.

    FYI, I deleted your first comment attempt because it didn’t come through properly as you indicated.

    Kirk out.

Leave a comment