Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to print object than Write-Host

I use Write-Host analyze objects, but some times it is hard to understand what the object actually is.

Consider:

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

Prints:

# Actually it prints nothing

I would like some thing like this:

Null
@()
@(Null, Null)

Any suggestions?

like image 885
alex2k8 Avatar asked Apr 24 '09 16:04

alex2k8


People also ask

What is the difference between write-host and write-output?

In a nutshell, Write-Host writes to the console itself. Think of it as a MsgBox in VBScript. Write-Output , on the other hand, writes to the pipeline, so the next command can accept it as its input. You are not required to use Write-Output in order to write objects, as Write-Output is implicitly called for you.

What are the differences between write-Host & write-output in PowerShell?

Write-Host uses the ToString() method to write the output. By contrast, to output data to the pipeline, use Write-Output or implicit output. You can specify the color of text by using the ForegroundColor parameter, and you can specify the background color by using the BackgroundColor parameter.

Can you print an object in Java?

The print(Object) method of PrintStream Class in Java is used to print the specified Object on the stream. This Object is taken as a parameter. Parameters: This method accepts a mandatory parameter object which is the Object to be printed in the Stream. Return Value: This method do not returns any value.


2 Answers

For this specific example, you can easily get what you want by sticking them in the property of an object. For the sake of an example, lets create an array with your three tests in it:

$tests = @($null,@(), @($null,$null))

function Write-Visible {
   param($InputObject) 

   New-Object PSObject -Property @{ Object=$InputObject } | 
       Out-String | Out-Host 
}

Of course, the Out-String | Out-Host stuff is just to make sure we don't actually output the objects to the pipeline, but behave like Write-Host does.

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 with that is that it tends not to work so great for real objects because it's turning them into properties of an object, where they get rendered "ToString()" ... however, off the top of my head, I can't think how to invoke the rendering magic that happens there without the new object.

like image 193
Jaykul Avatar answered Sep 22 '22 02:09

Jaykul


You can write a function that does the pretty-printing for you. Something like the following might work for your needs:

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 has, however still problems with an empty array on the shell (works fine from a .ps1 file, though). Also Hashtables aren't supported, but nested arrays are. Probably still needs some plumbing but might give a general direction.

The @($null, $null) array seems to be an ugly beast, resisting even comparing it to $null. Weird.

like image 34
Joey Avatar answered Sep 21 '22 02:09

Joey