When PowerShell outputs data to the console, the type of that data is not always obvious. That can sometimes lead to frustrating situations. While it's easy enough to check object types manually, it can sometimes be nice to see that information by default. We can accomplish that with a couple slight
Out-Default: The code in typetoggle.ps1 defines a pair of toggle functions. Executing the
typesonfunction will override the
Out-Defaultcmdlet so that it stores output type and length in global variables.
- Display prompt information: Within the
promptfunction of your
$profile, use the code from prompt.ps1 to display type information.
The prompt will only display type information if the last command includes a call to
Out-Default. This usually occurs implicitly at the end of the PowerShell pipeline.
Directory listing with 9 items
Last Output Type: System.Object (Length: 9)
Single user returned from the ActiveDirectory module's
Last Output Type: Microsoft.ActiveDirectory.Management.ADUser
Last Output Type: System.Object (Length: 44)
Get-Content <filename> -Raw
Last Output Type: System.String (Length: 1327)
Those last two examples really show where something like this can be useful. From the console, the output from both commands appears to be the same. When you check the type, you can see that one command returns an array of 44 strings while the other returns a single 1327-character string. If you're doing any sort of work with that output, that's good information to have.
When I realized that this sort of behavior would be useful, I searched around to see how other people had done it. The implementation that came closest to what I wanted was here. So thank you, Andy Schneider, five and a half years after the fact!