Better way to print an object than Write-Host

I use Write-Host analysis objects, but several times it’s hard to understand what the object really is.

Consider:

Write-Host $null Write-Host @() Write-Host @($null, $null) 

Fingerprints:

 # Actually it prints nothing 

I would like something like this:

 Null @() @(Null, Null) 

Any suggestions?

+6
pretty-print powershell
source share
5 answers

In this particular example, you can easily get what you want by pasting them into an object property. For example, let's create an array with your three tests:

 $tests = @($null,@(), @($null,$null)) function Write-Visible { param($InputObject) New-Object PSObject -Property @{ Object=$InputObject } | Out-String | Out-Host } 

Of course the material is Out-String | Out-Host Out-String | Out-Host is to make sure that we do not actually output objects to the pipeline, but behave like Write-Host.

So now we can run our tests:

 PS> Write-Visible $tests[0] Object ------ PS> Write-Visible $tests[1] Object ------ {} PS> Write-Visible $tests[2] Object ------ {$null, $null} 

Of course, the problem is that it does not work so well for real objects, because it turns them into properties of the object, where they get the rendered "ToString ()" ... however, from above my head, I can’t think how call the rendering manner that happens there without a new object.

+5
source share

You can write a function that does pretty print for you. For your needs, the following may work:

 function pp($a) { if ($a -eq $null) { return "Null" } elseif ($a -is [object[]]) { $b = @() foreach ($x in $a) { $b += (pp $x) } $s = "@(" + [string]::Join(",", $b) + ")" return $s } else { return $a } } 

This, however, is still a problem with an empty array in the shell (works fine from a .ps1 file). Also hashtables are not supported, but nested arrays. Probably some plumbing is still required, but it can give a general direction.

The @($null, $null) array @($null, $null) seems like an ugly beast without comparing it to $null . Weird

+2
source share

There will be a few problems trying to do something like this.

  • The “right” or “powershell” way to change the recording node is to use formatting files to determine how you want the host you are using to display the objects in question. You can get more information from get-help about_format.ps1xml or from the MSS PowerShell Formatting File page .
  • You need to be specific to the "host" that you are using. See get-help get-host for more information or the TechNet page . There are three main hosts you want to know about:
    • main command line, ConsoleHost
    • basic version of PowerShell ISE, Windows PowerShell ISE Host
    • Quest node PowerGUI, PowerGUIScriptEditorHost
  • PowerShell handles @ () syntax in a special way, which sometimes makes it difficult to work with an empty array. According to this MSS PowerShell blog post :

    ... operation

    @( … )

    - syntactic sugar for

    [array] $( … )

    So - if the operators in @ () return a scalar, it will be wrapped in an array but if the result is already an array, then it will not be nested ...

I would like to provide a code for this, but it is a bit superior to me at this point ...

+1
source share

It does not work for empty arrays, but you can use one of the format-xxx commands. This helps format objects that do not have a useful ToString override. For example.

 > $cmd = Get-Command -Name get-command > $cmd CommandType Name Definition ----------- ---- ---------- Cmdlet Get-Command Get-Command... > write-host $cmd Get-Command > format-table -InputObject $cmd | out-string | out-host CommandType Name Definition ----------- ---- ---------- Cmdlet Get-Command Get-Command... 
+1
source share

You can try the following:

 Write-Host '$null' Write-Host '@()' Write-Host '@($null, $null)' 
-one
source share

All Articles