In the unrelated picture, look, I have started to learn how to draw!

This evening, I dug myself out of a PowerShell trap that has caught me dozens of times: leaving values on the pipeline. Ouch. That hurt, but I survived to tell the tale with a contrived example...

function Hash-ContentBad ($Content) {
    New-Item "./temp" -Force;
    Add-Content -Value $Content -Path "./temp";
    $result = Get-FileHash "./temp";
    return $result.Hash;
}
function Hash-ContentGood ($Content) {
    New-Item "./temp" -Force | Out-Null;
    Add-Content -Value $Content -Path "./temp";
    $result = Get-FileHash "./temp";
    return $result.Hash;
}

I found myself flailing for about an hour before I realized I had fallen into the trap, again! What a sneaky trap!

Why does the bad one contain all that output?

Of course in the contrived example this seems obvious. Why would we wonder? It seems obvious. But in a scripting environment with lots of complexity dragons live here in this trap! Why, our variable contains an Object[] instead of the expected string! But no matter how hard we look at $result.Hash, it looks like a string. A string! That $result.Hash value contains a string! What magic happens to make that $result.Hash turn into an Object[] when it crosses the threshold of the function?!?!?!

Oh. Right. That trap. I fell into that trap.

Avoid the Trap! Pipe to Out-Host! Pipe to Out-Null! Save yourself!!!