unable to export data from loop

I want to export the username and what group it was removed from to csv. I can not figure out the proper syntax to do this .

 

Here is the code . I have tried to comment as much as i could. The code removes the users from groups and should add the results to the csv file. i have the write host that is telling me it is doing it but the results are not going to csv. No matter where i put the code or how i do it. Since this is a list it will be multiple users that will have to be in the csv file.

 

 

 

$users = Import-Csv -Path “C:\users\public\disabledUserRemoveFromGroup\DisabledUsers.CSV”

Foreach ($username in $users) {

#$username = $users.SamAccountName

Get all group memberships

$groups = get-adprincipalgroupmembership $username.SamAccountName;

Loop through each group

$data = foreach ($group in $groups) {

Exclude Domain Users group

if ($group.name -ne “domain users”) {

Remove user from group

get-adgroup $group | remove-adgroupmember -member $username.SamAccountName -Confirm:$false

Write progress to screen

write-host $username.SamAccountName "have been remove from the group - " $group.name

} $data |export-csv c:\Users\public\results.csv -Append

}
}

Hello Matthew,

First thing is the way you are assigning the output of everything in the foreach to $data, you may end up with unexpected information. You may not even know it’s happening without digging deeper. What I recommend is collecting each user/group combination that you act on in a new PSObject and then at the end exporting it all to csv at the end.

$users = Import-Csv -Path “C:\temp\DisabledUsers.CSV”

$data = New-Object System.Collections.ArrayList

Foreach ($username in $users) {

#$username = $users.SamAccountName

# Get all group memberships
$groups = get-adprincipalgroupmembership $username.SamAccountName;

# Loop through each group
foreach ($group in $groups) {

# Exclude Domain Users group
if ($group.name -ne "domain users") {

[void]$data.add($(
new-object -typename psobject -property @{
Group = $($Group.name)
User = $username.samaccountname
}))
# Remove user from group
#get-adgroup $group | remove-adgroupmember -member $username.SamAccountName -Confirm:$false

# Write progress to screen
write-host $username.SamAccountName "have been remove from the group - " $group.name

}

}
}
$data | Export-Csv c:\Users\public\results.csv -Append -NoTypeInformation

Here is the CSV from my test

User Group
test Monitor C Drive
test Target
btest DG-Management
btest Monitor C Drive

 

I hope this helps.

Hi Matthew,

I’m not sure if my browser messed up or if my answer is being moderated. Just in case i’ll post it again. The way you’re assigning all the output of foreach group to $data, you may end up for more than you bargained for. What I recommend is controlling what is put in the $data variable via a new PSObject that contains each user/group combo that was acted upon. After collecting all the results you can export to CSV.

$users = Import-Csv -Path “C:\temp\DisabledUsers.CSV”

$data = New-Object System.Collections.ArrayList

Foreach ($username in $users) {

#$username = $users.SamAccountName

# Get all group memberships
$groups = get-adprincipalgroupmembership $username.SamAccountName;

# Loop through each group
foreach ($group in $groups) {

# Exclude Domain Users group
if ($group.name -ne "domain users") {

[void]$data.add($(
new-object -typename psobject -property @{
Group = $Group.name
User = $username.samaccountname
}))
# Remove user from group
#get-adgroup $group | remove-adgroupmember -member $username.SamAccountName -Confirm:$false

# Write progress to screen
write-host $username.SamAccountName "have been remove from the group - " $group.name

}

}
}
$data | Export-Csv c:\Users\public\results.csv -Append -NoTypeInformation

Here is the CSV content from my test

Get-Content C:\users\Public\results.csv
"User","Group"
"test","Monitor C Drive"
"test","Target"
"btest","DG-Management"
"btest","Monitor C Drive"

I hope this helps.

Wow. I have been working on this all morning and your solution is simple. Thank you so much.

I guess i will have to do some reading on array list and how to use it. I have never seen it done this way before.

 

Thank you again.

i have one more quick queston. Since this is new too me I added another property that i wanted - DN. it shows up on the report but i wanted to be in a different order. I guess the OCD in me.

the report now shows - Group , Username , DN

I would like to show up as Username , Group, DN

I tried changing the order as below but that did not help. Thank you in advance. I am not sure what happen to the users reply either. ODD

 

 

[void]$data.add($(
new-object -typename psobject -property @{
User = $username.samaccountname
Group = $($Group.name)
DN = $username.DistinguishedName
}))

What you need is a ordered hashtable, which I believe didn’t come about until version 3.

$oht = [ordered]@{
User = $username.samaccountname
Group = $Group.name
DN = $username.distinguishedname
}
[void]$data.add($(new-object -typename psobject -property $oht))

This should do what you want, as long as you’ve populated DisabledUsers.csv with a column of data titled DistinguishedName

 

Odd. Now it is only showing the last result on the list only. If i have more than one it will not show it. Maybe it is replication. I will wait a bit. Thank you so much. This is helping me automate a lot of stuff.

thanks mate. It was replication. It is worked as expected.

No worries, I’ll get you the address where you can automate a portion of your income my way. :wink: