Wiktor
Wiktor

Reputation: 631

Export-csv return 1 instance instead a whole resutls

Don't know why Exporting csv return only 1 object instead the whole result. Why is that if function is in the loop ?

$names = Import-CSV C:\PowerShell\UpdatePhone\Telefony_1.csv -Header Givenname,Surname -Delimiter ","
ForEach ($Name in $Names)
{
    $FirstFilter = $Name.Givenname
    $SecondFilter = $Name.Surname
    Foreach-object{
    Get-ADUser -Properties * -Filter { GivenName -like $FirstFilter -and Surname -like $SecondFilter}|select samaccountname,name,employeeID,mail,ipphone, mobile| Export-Csv C:\PowerShell\UpdatePhone\Telefony.csv
}}  

Regards

Upvotes: 1

Views: 45

Answers (1)

Ranadip Dutta
Ranadip Dutta

Reputation: 9133

Because each time it is creating/exporting a new file with the last object. You should use -append

Change this

Get-ADUser -Properties * -Filter { GivenName -like $FirstFilter -and Surname -like $SecondFilter}|select samaccountname,name,employeeID,mail,ipphone, mobile| Export-Csv C:\PowerShell\UpdatePhone\Telefony.csv

To:

Get-ADUser -Properties * -Filter { GivenName -like $FirstFilter -and Surname -like $SecondFilter}|select samaccountname,name,employeeID,mail,ipphone, mobile| Export-Csv C:\PowerShell\UpdatePhone\Telefony.csv -Append

PS: I have not gone through the logic that you are persisting for doing so. I just corrected the fundamental problem.

Upvotes: 2

Related Questions