Skip to content

@ajkerrigan /README.md
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Display type info for the last command's output in your PowerShell prompt

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 $profile modifications:

  • Override Out-Default: The code in typetoggle.ps1 defines a pair of toggle functions. Executing the typeson function will override the Out-Default cmdlet so that it stores output type and length in global variables.
  • Display prompt information: Within the prompt function 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.

Examples

Directory listing with 9 items

Last Output Type: System.Object[] (Length: 9)

Single user returned from the ActiveDirectory module's Get-ADUser cmdlet

Last Output Type: Microsoft.ActiveDirectory.Management.ADUser

Results of Get-Content <filename>

Last Output Type: System.Object[] (Length: 44)

Results of 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.

Acknowledgements

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!

if ($Global:LastOutputType) {
$lastOut = "Last Output Type: $($Global:LastOutputType.FullName)"
if ($Global:LastOutputLength) {
$lastout += " (Length: $($Global:LastOutputLength))"
}
Write-Host $lastOut
}
function typeson {
if (Test-Path Function:Global:Out-Default) {
Write-Warning "Output type information is already enabled. No action required."
}
else {
New-Item -ItemType Function -Path Function:Global:Out-Default -Value {
$Input | Tee-Object -Variable output | Microsoft.PowerShell.Core\Out-Default
if ($output) {
$Global:LastOutputType = $output.GetType()
if ($Global:LastOutputType.GetProperty("Length")) {
$Global:LastOutputLength = $output.Length
}
else {
$Global:LastOutputLength = $null
}
}
else {
$Global:LastOutputType = $null
$Global:LastOutputLength = $null
}
} | Out-Null
Write-Host "Output type information in the prompt is now enabled."
}
}
function typesoff {
if (Test-Path Function:Out-Default) {
Remove-Item Function:Out-Default | Out-Null
$Global:LastOutputType = $null
$Global:LastOutputLength = $null
Write-Host "Output type information has been removed from the prompt."
}
else {
Write-Warning "Output type information in the prompt is not enabled. No action required."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.