Avisekh Das
Avisekh Das

Reputation: 161

Foreach loop is not giving the complete result

I am trying to get the ownership details of a subfolder of a folder which is has 139 folders. I have made my own script to get the details. But I am wondering why it is always 93 folders in the csv where the total number of subfolders is 139.

$folder = "path"
$user = Get-ChildItem $folder
foreach ($user in $users)
{
    get-acl $users.fullname | export-Csv f:\sha.csv 
}

Upvotes: 0

Views: 62

Answers (1)

Ranadip Dutta
Ranadip Dutta

Reputation: 9133

Instead of $Users inside the foreach loop, you should use $user and in the CSV you should append the data each time rather than creating a new one.

$folder = "path"
$user = Get-ChildItem $folder
foreach ($user in $users)
{
get-acl $user.fullname | export-Csv f:\sha.csv -Append -Force
}

If you are using $users directly then there is no point in using foreach. $User is the loop variable , so each value from $users will come to $user one by one and whatever you are doing from inside the loop is gonna work on that.

Hope it helps.

Upvotes: 1

Related Questions