Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, September 12, 2019 7:29 PM
The below script is getting the content of the c:\computers.txt that has a list of computer names one after another. Then the for each loop is just checking if each computer object exists in AD or not.
That works, but the problem is, when I do an export to CSV to c:\result.csv, it is writing the last computer in the c:\computers.txt indicating that it is most likely just overwriting the first line over and over again instead of writing a new line for each computer object.
I tried the 'r'n , biut that didn't help. Any help would be greatly appreciated.
$computers = get-content c:\Computers.txt
ForEach($computer in $computers){
get-ADComputer $computer | Select-Object -Property name,DistinguishedName | export-csv c:\result.csv -notypeinformation
}
All replies (3)
Thursday, September 12, 2019 8:46 PM ✅Answered
Use the pipeline correctly and this won't happen:
Get-Content c:\Computers.txt |
Get-ADComputer |
Select-Object Name, DistinguishedName |
Export-Csv c:\result.csv -notypeinformation
\(ツ)_/
Friday, September 13, 2019 7:46 AM | 1 vote
Hi,
Thanks for your question.
You can use "-append " parameter: Use this parameter so that Export-CSV adds CSV output to the end of the specified file. Without this parameter, Export-CSV replaces the file contents without warning.
$computers = get-content c:\Computers.txt
ForEach($computer in $computers){
get-ADComputer $computer | Select-Object -Property name,DistinguishedName | export-csv c:\result.csv -notypeinformation -append
}
Or:
$computers = get-content c:\Computers.txt
$result=ForEach($computer in $computers){ get-ADComputer $computer | Select-Object -Property name,DistinguishedName }
$result | export-csv c:\result.csv -notypeinformation
/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-6
Best regards,
Lee
Just do it.
Friday, September 13, 2019 7:49 AM
Hi,
Thanks for your question.
You can use "-append " parameter: Use this parameter so that Export-CSV adds CSV output to the end of the specified file. Without this parameter, Export-CSV replaces the file contents without warning.
Which is slow, inefficient and requires more code than necessary.
\(ツ)_/