How to export powershell code to csv correct column

I need to export the result to CSV. Can someone guide me on how to do it

$Inventory = Get-Content -Path 'C:\Users\tdadmin\Desktop\hostname.txt'
foreach ($computer in $Inventory) {
    Get-ADComputer -Identity $computer -Properties * | FT Name, LastLogonDate -Autosize
}

Upvotes: 0

Views: 108

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200483

Do not use Format-* cmdlets unless you want to present data directly to a user. For selecting particular properties of your input objects use Select-Object. Use a pipeline and remove the loop (Get-ADComputer accepts pipeline input). Use Export-Csv for actually exporting the data to a CSV.

Get-Content -Path 'C:\Users\tdadmin\Desktop\hostname.txt' |
    Get-ADComputer -Properties * |
    Select-Object Name, LastLogonDate |
    Export-Csv 'C:\path\to\output.csv -NoType

Upvotes: 4

Related Questions