PowerShell script to return members of multiple security groups

后端 未结 4 1737
感情败类
感情败类 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 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
    

提交回复
热议问题