Powershell read from CSV set variables

The second foreach has no input object to iterate over. So either

  • import to a variable and pipe it twice to the foreach loops
  • import twice

$csv = import-csv c:\test\output.csv 
$csv | foreach-object {
  $Path = $_.path
  $owner =$_.owner
  "The username is $Path and the owner is $owner"
}
$csv | ForEach-Object {
    $Path = $_.path
    $owner =$_.owner
    $Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList "$owner"
    $Acl = Get-Acl -Path $path
    $Acl.SetOwner($Account);
    Set-Acl -Path $Path -AclObject $Acl
}

Tags:

Csv

Powershell