0

I have a csv file that has a lot of data in it, with one column being a "Username" column. I created a loop to query AD and get each username and now I need to export each of those names to the specific column in the csv. After importing the csv with:

$data = Import-CSV .\data.csv

And using the loop:

foreach($user in $data)

And I use get-aduser $user -server $server and if($? -eq $true){ $user = $user + "01" }

I tried using $data.Username | Export-CSV .\data.csv and $data.Username | out-file .\data.csv but so far neither have worked.

2
  • What's inside the loop? You've described your script but left out the part where you're actually manipulating the data. It sounds like you have a complete script which "doesn't work" - show the code and explain what exactly "doesn't work." Commented Mar 15, 2013 at 13:24
  • You shouldn't be calling export-csv on each item. You should create a new collection of objects, and call export-csv on that. Commented Mar 15, 2013 at 13:30

1 Answer 1

1

You need to keep all the information in the pipeline so you can re-export the whole thing.

Try something like this:

$data | Foreach {
 get-aduser $_.Username -server $server
 if($? -eq $true){ $_.Username = $_.Username + "01" }
} | export-csv .\data.csv 
Sign up to request clarification or add additional context in comments.

Comments

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.