如何将Powershell代码导出到CSV正确列

时间:2018-12-19 23:29:31

标签: powershell active-directory

我需要将结果导出到CSV。有人可以指导我怎么做

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

1 个答案:

答案 0 :(得分:4)

请勿使用Format-* cmdlet,除非要将数据直接呈现给用户。要选择输入对象的特定属性,请使用Select-Object。使用管道并删除循环(Get-ADComputer接受管道输入)。使用Export-Csv实际将数据导出到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