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.