I\'m working on a basic PowerShell script that inputs a pair of dates then gets all accounts with passwords expiring between those times. I\'d like to output the data to the con
Replace
Write-Output ($accts | Format-Table | Out-String)
with
$accts
That way your users can run your script any way they like, e.g.
.\your_script.ps1 | Format-Table
.\your_script.ps1 | Format-List
.\your_script.ps1 | Export-Csv
.\your_script.ps1 | Out-GridView
...
Format-Table | Out-String
converts your output to a single string whereas Export-Csv
expects a list of objects as input (the object properties then become the columns of the CSV). If Export-Csv
is fed a string, the only property is Length
, so you get a CSV with one column and one record.
$accts | ConvertTo-Csv | Tee -File output.csv | ConvertFrom-Csv