PowerShell script to return members of multiple security groups

后端 未结 4 1721
感情败类
感情败类 2021-01-17 21:32

I need to return all members of multiple security groups using PowerShell. Handily, all of the groups start with the same letters.

I can return a list of all the rel

相关标签:
4条回答
  • 2021-01-17 21:49
    Get-ADGroupMember "Group1" -recursive | Select-Object Name | Export-Csv c:\path\Groups.csv
    

    I got this to work for me... I would assume that you could put "Group1, Group2, etc." or try a wildcard. I did pre-load AD into PowerShell before hand:

    Get-Module -ListAvailable | Import-Module
    
    0 讨论(0)
  • 2021-01-17 21:50

    This will give you a list of a single group, and the members of each group.

    param
    (   
        [Parameter(Mandatory=$true,position=0)]
        [String]$GroupName
    )
    
    import-module activedirectory
    
    # optional, add a wild card..
    # $groups = $groups + "*"
    
    $Groups = Get-ADGroup -filter {Name -like $GroupName} | Select-Object Name
    
    ForEach ($Group in $Groups)
       {write-host " "
        write-host "$($group.name)"
        write-host "----------------------------"
    
        Get-ADGroupMember -identity $($groupname) -recursive | Select-Object samaccountname
    
     }
    write-host "Export Complete"
    

    If you want the friendly name, or other details, add them to the end of the select-object query.

    0 讨论(0)
  • 2021-01-17 22:01

    This is cleaner and will put in a csv.

    Import-Module ActiveDirectory
    
    $Groups = (Get-AdGroup -filter * | Where {$_.name -like "**"} | select name -expandproperty name)
    
    
    $Table = @()
    
    $Record = [ordered]@{
    "Group Name" = ""
    "Name" = ""
    "Username" = ""
    }
    
    
    
    Foreach ($Group in $Groups)
    {
    
    $Arrayofmembers = Get-ADGroupMember -identity $Group | select name,samaccountname
    
    foreach ($Member in $Arrayofmembers)
    {
    $Record."Group Name" = $Group
    $Record."Name" = $Member.name
    $Record."UserName" = $Member.samaccountname
    $objRecord = New-Object PSObject -property $Record
    $Table += $objrecord
    
    }
    
    }
    
    $Table | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation
    
    0 讨论(0)
  • 2021-01-17 22:10

    If you don't care what groups the users were in, and just want a big ol' list of users - this does the job:

    $Groups = Get-ADGroup -Filter {Name -like "AB*"}
    
    $rtn = @(); ForEach ($Group in $Groups) {
        $rtn += (Get-ADGroupMember -Identity "$($Group.Name)" -Recursive)
    }
    

    Then the results:

    $rtn | ft -autosize
    
    0 讨论(0)
提交回复
热议问题