so I have an object
$appstatus=[pscustomobject] @{
txtlist=@()
csvlist=@()
someotherproperties
}
and a function which either loads TXT or imports CSV file. Depending on the filename chosen it fills in one property of $appstatus object. Then I have another function to show currently loaded list. Something like
function showhosts(){
if(($appstatus.txtlist).count -gt 0){
write-host $appstatus.txtlist
}else{
write-host $appstatus.csvlist
}
}
txtlist is fine, but the problem is with csvlist because write-host does not show the nice table format but this @{property=value; ...} long string instead. I cannot just type $appstatus.csvlist without write-host, because that would NOT be displayed and become return value of a function instead, so how can I display the object nicely from within a function the same way as if it was called from the main script?
Format-Table?| ft? Formats pipeline as tableOut-Host-->$appstatus.txtlist | out-hostWrite-Hostperforms simple.ToString()stringification (a single-line representation that is often unhelpful), whereasOut-Hostuses PowerShell's rich for-display formatting system.Write-Hostoutput cannot be piped, because it doesn't go to PowerShell's (success) output stream, it (effectively) goes straight to the host (display). Note that not writing to the output stream is explicitly desired by the OP.