Roland van Zanten
Roland van Zanten

Reputation: 3

How to get the get-ADPrincipalGroupMembership for all users in a txt or csv file and put into a txt file for each user?

I am trying to get a file with the group-memberships for every user that is specified in a txt/csv file. so this is what i had before:

Get-ADPrincipalGroupMembership -Identity $user -Server $DC | Select name | Where-Object name -like GUSR_* | Out-File "C:\temp\$user.txt" 

this work fine for getting the groups from 1 singel user, but now i have to do this for 100+ users. And instead of doing it one by one i am looking for a way to automate it.

so i got myself a .csv export of all the users i want this done for. and started trying.

what i came up with so far:

$users = Get-Content "C:\temp\test.csv" |ForEach-Object  {Get-ADPrincipalGroupMembership -Identity $users -Server $DC | Select name | Where-Object name -like GUSR_* | Out-File "\\ads.net\ADS\SDL\Temp\_ROLAND\RSD\test2\$users.txt"}

This cleary doesnt work. I have tried a couple of other things with the foreach command but nothing did the trick. I have the feeling i am not on the right path to get my result. Maby somebody has done this before and can help me get on the right path. i'm not new to powershell but i'm far from an expert, most of the time i use it for basic singel commands or edit some great scripts i find. sadly for this i haven't found any yet.

with kind regards Roland

Upvotes: 0

Views: 3427

Answers (1)

wasif
wasif

Reputation: 15526

  • Don't assign back to a variable
  • Import the CSV
  • No filter after select
  • Pretiffy your -like
  • Use $_ as pipeline variable
  • Use subexpression operator for string+variable concatenation
Import-Csv "C:\temp\test.csv" |ForEach-Object  {Get-ADPrincipalGroupMembership -Identity $_.users -Server $_.DC | Where-Object {$_.name -like 'GUSR_*'} | Select -Expand Name | Out-String | Out-File "\\ads.net\ADS\SDL\Temp\_ROLAND\RSD\test2\$($_.users).txt"}

Upvotes: 1

Related Questions