3

I have a CSV File that has two columns Employee and Manager and I would like to import them into two Variables for use later in my script. However when I run my code it only captures the last data item in the CSV as the previous one is being over written.

$CSVFiles = Import-CSV "C:\T2\EmployeeManager.csv"

ForEach($CSVFile in $CSVFiles)
{
  $Employee = ($CSVFile.Employee); $Manager = ($CSVFile.Manager)
}
JRN
  • 269
  • 1
  • 3
  • 19

4 Answers4

6
$Employee = $CSVFiles | Select -Expand Employee
$Manager  = $CSVFiles | Select -Expand Manager
iRon
  • 20,463
  • 10
  • 53
  • 79
3

That's because you are overwriting the added data each time the loop runs. IN PowerShell the += appends to an object. Try this -

$Employee = @()
$Manager = @()

$CSVFiles = Import-CSV "C:\T2\EmployeeManager.csv"

ForEach($CSVFile in $CSVFiles)
{
  $Employee += $CSVFile.Employee; $Manager += $CSVFile.Manager
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27
  • 2
    That explains the OP's problem well and your solution works, but I suggest adding a performance warning re use of `+=` for "extending" an array iteratively in a loop. – mklement0 Jul 11 '18 at 17:18
2

Vivek Kumar Singh's helpful answer explains the problem with your approach well and offers a solution.

Here's a simpler alternative (PSv3+), given that you're loading the entire CSV file into (custom objects) into memory anyway:

$CSV = Import-CSV "C:\T2\Employee.csv"

$Employees = $CSV.Employee  # collect the Employee column values across all input rows
$Managers  = $CSV.Manager   # ditto for Manager

This approach takes advantage of the PSv3+ member-access enumeration feature.

In PSv2, use iRon's helpful solution.


Comparing the performance of the solutions:

  • The member-enumeration solution in this answer is fastest,
  • followed by iRon's Select -Expand solution
  • with Vivek's foreach loop being the slowest by far, not least because use of += to (conceptually) extend an array requires creating a new instance behind the scenes in every iteration.
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

This ended up being my final coding to accomplish my end goal. I want to thank all the posters I used a little learning from all of you to come to the solution I wanted.

$EmployeeLists = @()
$ManagerLists = @()
$Employees = @()
$Managers = @()


$CSVFiles = Import-CSV "C:\T2\EmployeeManager.csv"

ForEach($CSVFile in $CSVFiles)
{
  $EmployeeLists += ($CSVFile.Employee)
}

ForEach($CSVFile in $CSVFiles)
{
  $ManagerLists += ($CSVFile.Manager)
}

$Employees += ForEach ($EmployeeList in $EmployeeLists) { Get-ADUser -Properties * -Filter { DisplayName -like $EmployeeList } | Select SamAccountName -ExpandProperty SamAccountName }
$Managers += ForEach ($ManagerList in $ManagerLists) { Get-ADUser -Properties * -Filter { DisplayName -like $ManagerList } | Select SamAccountName -ExpandProperty SamAccountName }


$EmployeeLists
"`n`n`n"
$Employees
JRN
  • 269
  • 1
  • 3
  • 19