简体   繁体   中英

Powershell ALL AD Users and their Group Memberships

Looking to write a powershell script that will pull ALL AD users, their group memberships and the groups Description Field.

I have been using two scripts to accomplish this, and just manually manipulating them in excel. Any attempt to combine them has been met with error.

 Import-module activedirectory
$ou ="DC=blah,DC=blah"
Get-ADGroup -Filter * -SearchBase $OU  | select -expandproperty name | % {
$group= "$_"
$result += Get-ADGroupMember -identity "$_" | select @{n="Group";e={$group}},name
}



$result | export-csv 'c:\users\membership.csv' -notypeinformation

And:

    Import-Module ActiveDirectory
$Groups = ForEach ($G in (Get-ADGroup -Filter * ))
{

$UN = Get-ADGroup $G -Properties Description | select name, description


New-Object PSObject -Property @{
Desc=$UN.description
Name=$UN.name
}

}

$Groups | Export-CSV C:\users\GroupDesc.csv -notypeinformation

I hope i've got this right, this will pull all users from AD and get the groups each one is a member of (including the groups description). After everything is done it puts the info into a csv.

Import-Module ActiveDirectory

$OU = "DC=blah,DC=blah"

#$allUsers = Get-ADUser -LDAPFilter "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))" # all users that are enabled
#$allUsers = Get-ADUser -Filter * # all users
$allUsers = Get-ADUser -Filter * -SearchBase $OU
$results = @()

foreach($user in $allUsers)
{

    $userGroups = Get-ADPrincipalGroupMembership -Identity $user
    foreach($group in $userGroups)
    {
        $adGroup = Get-ADGroup -Identity $group -Properties Description
        $results += $adGroup | Select-Object -Property @{name='User';expression={$user.sAMAccountName}},Name,Description
    }
}
$results | Export-Csv -Path 'C:\Membership.csv' -NoTypeInformation -Encoding Unicode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM