Skip to main content
added 54 characters in body
Source Link
Graham Gold
  • 2.5k
  • 2
  • 30
  • 36

The short answer is to add the -Append option to your export-csv statement to stop it overwriting each time round the loop.

Alternatively move the export outside the loop as follows:

$managers = Import-Csv -Path .\test.csv

$managers|foreach-object{
  $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" | select -ExpandProperty SAMAccountName
  $_|Select *,@{Name='managerUsername';Expression=$getmanagersam}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation

Note: looks like @AnsgarWiechers beat me to it :-)

The short answer is to add the -Append option to your export-csv statement to stop it overwriting each time round the loop.

Alternatively move the export outside the loop as follows:

$managers = Import-Csv -Path .\test.csv

$managers|foreach-object{
  $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" | select -ExpandProperty SAMAccountName
  $_|Select *,@{Name='managerUsername';Expression=$getmanagersam}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation

The short answer is to add the -Append option to your export-csv statement to stop it overwriting each time round the loop.

Alternatively move the export outside the loop as follows:

$managers = Import-Csv -Path .\test.csv

$managers|foreach-object{
  $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" | select -ExpandProperty SAMAccountName
  $_|Select *,@{Name='managerUsername';Expression=$getmanagersam}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation

Note: looks like @AnsgarWiechers beat me to it :-)

added 450 characters in body
Source Link
Graham Gold
  • 2.5k
  • 2
  • 30
  • 36

The short answer is to add the -Append option to your export-csv statement to stop it overwriting each time round the loop.

Alternatively move the export outside the loop as follows:

$managers = Import-Csv -Path .\test.csv

$managers|foreach-object{
  $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" | select -ExpandProperty SAMAccountName
  $_|Select *,@{Name='managerUsername';Expression=$getmanagersam}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation

The short answer is to add the -Append option to your export-csv statement.

The short answer is to add the -Append option to your export-csv statement to stop it overwriting each time round the loop.

Alternatively move the export outside the loop as follows:

$managers = Import-Csv -Path .\test.csv

$managers|foreach-object{
  $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" | select -ExpandProperty SAMAccountName
  $_|Select *,@{Name='managerUsername';Expression=$getmanagersam}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation
Source Link
Graham Gold
  • 2.5k
  • 2
  • 30
  • 36

The short answer is to add the -Append option to your export-csv statement.