I have a piece of code that I wrote which captures some data and write it to a CSV file. I have written it two way. 1) The first way only gives me the last result rather than all the results almost as if it was over writing itself. 2) The second way gives me an error that says "An Empty Pipe Element Is Not Allowed"
WAY 1)
foreach ($Computer in $CompObjToRM)
{
Get-ADComputer -Identity $Computer | Select Name, DistinguishedName | Export-CSV C:\T2\ServersToRemoveFromAD2.csv -NoTypeInformation
}
WAY 2)
foreach ($Computer in $CompObjToRM)
{
Get-ADComputer -Identity $Computer | Select Name, DistinguishedName
} | Export-CSV C:\T2\ServersToRemoveFromAD2.csv -NoTypeInformation
What am I doing wrong?
| Out-Fileis a command on its own. I get you were trying to pipe theforeachintoOut-Filebut that's not how those brackets{ }work. The brackets simply indicate the start and end of theforeach. No data will be output, per se, without you telling it how to.