AD Groups Powershell

Eric Orosz 61 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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,856 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
    

1 additional answer

Sort by: Most helpful
  1. Eric Orosz 61 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?


Your answer

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