Share via

AD Groups Powershell

Eric Orosz 131 Reputation points
2021-08-03T21:36:24.91+00:00

I would like to pull a list of user groups using the following code(Get-ADPrincipalGroupMembership 'username' | select -expand name | sort) | format-table -Property name then out-file it to a .txt file but before doing that I would also like to pull out the groups from the same list that start with 'DMS' in the name in a different .txt or the same one in as a two column format if possible.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2021-08-03T22:54:47.633+00:00

In simpler terms, you want a list of all the groups in which a user ("username") is a member. Is that right? Then put those names into two files, one for all groups whose name begins with "DMS" and another one for all groups show names don't begin with "DMS". Is that also right?

Something like this might be what you want (NOTE: I haven't run this!):

$dms = @()
$username = 'xyz'
$dmsfile = c:\junk\dmsgroups.txt
$nondmsfile = c:\junk\nondmsgroups.txt

Get-ADPrincipalGroupMembership -Identity $username |
    Select-Object -Property name |
        Sort-Object |
            ForEach-Object{
                if ($_.name -like 'DMS*'){
                    $dms += $_.name
                }
                else{
                    $_.name
                }
            } | Out-File $nondmsfile
$dms | Out-File $dmsfile

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Eric Orosz 131 Reputation points
    2021-08-04T00:25:52.647+00:00

    I had another question when creating a new user account in AD for the home folder creation is there a powershell script that I could run that after it creates the new user folder on the lets say H drive where it creates a subfolder in any new user folder called Recover?

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.