1

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?

3
  • Way 1 - Yes, your assumption is correct. Way 2 - | Out-File is a command on its own. I get you were trying to pipe the foreach into Out-File but that's not how those brackets { } work. The brackets simply indicate the start and end of the foreach. No data will be output, per se, without you telling it how to. Commented Jul 20, 2016 at 17:18
  • I think I might be an idiot I think i need to use -NoClobber in the Export-CSV command after piping the command results to my export-csv. Let me fix and run the code and then confirm or deny my stupidity. Commented Jul 20, 2016 at 17:20
  • Ok so when I code this: foreach ($Computer in $CompObjToRM) { Get-ADComputer -Identity $Computer | Select Name, DistinguishedName | Export-CSV -NoClobber C:\T2\ServersToRemoveFromAD2.csv -NoTypeInformation } I get and error: The file 'C:\T2\ServersToRemoveFromAD2.csv' already exists. Commented Jul 20, 2016 at 17:23

1 Answer 1

1

Either use the -Append switch parameter with Export-Csv to avoid it overwriting the same file:

foreach ($Computer in $CompObjToRM)
{
    Get-ADComputer -Identity $Computer | Select Name, DistinguishedName | Export-CSV C:\T2\ServersToRemoveFromAD2.csv -Append -NoTypeInformation
} 

or use the ForEach-Object cmdlet (from which you can pipe all the output to Export-Csv)

$CompObjToRM |ForEach-Object {
    Get-ADComputer -Identity $_ | Select Name, DistinguishedName 
} | Export-CSV C:\T2\ServersToRemoveFromAD2.csv -NoTypeInformation

The big difference here is that foreach($thing in $things){} is a builtin semantic feature of the language, whereas ForEach-Object is a cmdlet - a piece of reusable code that supports pipeline input and output.

ForEach-Object can be a bit slower (and behave slightly differently) than the foreach loop, but in most cases, it simply just offers you a greater degree of flexibility in composing your pipeline

Sign up to request clarification or add additional context in comments.

2 Comments

Very nice usage of the | before AND after the foreach call. I'd expand on that option, as the original poster was a bit confused why a post pipe didn't work as they expected! :-)
@gravity true, updated the answer with a few paragraphs (feel free to add)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.