Powershell to get group, members and member of details - powershell

I'm trying to use powershell to get a list of global groups in an OU and output the global group name, the members and the domain local groups the global group is a member of, so far I have the output below, but how do I get the output for the "member of" details
$OU = 'OU=Role Groups,OU=USG,OU=Groups,OU=xxx,OU=xxxxxx,DC=xxxxxxx,DC=xxx'
$Groups = Get-ADGroup -Filter * -SearchBase $OU
$Data = foreach ($Group in $Groups) {
Get-ADGroupMember -Identity $Group -Recursive | Select-Object #{Name='Group';Expression={$Group.Name}}, #{Name='Member';Expression={$_.Name}}
}
$Data | Export-Csv -Path "C:\Temp\FolderPermissions.csv"

Why not take a more direct approach as defined in the help files?
Get-ADGroup
Get-ADGroupMember
Get-ADGroup |
Where-Object {GroupScope -eq 'Global'} |
Get-ADGroupMember
Or
$OU = 'OU=Role Groups,OU=USG,OU=Groups,OU=xxx,OU=xxxxxx,DC=xxxxxxx,DC=xxx'
Get-ADGroup -Filter "GroupScope -eq 'Global'" -SearchBase $OU |
Get-ADGroupMember | Select-Object -Property SamAccountName

Related

How to get-adgroup members by their Name or SamAccountName

i would like to extract members from an AD Group that contains Members and security group.
Example, Group_A:
User1
User2
User3
Group_B
When I run my script, it shows:
CN=User1,OU=Users,DC=Contoso,DC=com
CN=User2,OU=Users,DC=Contoso,DC=com
CN=User3,OU=Users,DC=Contoso,DC=com
CN=Group_B,OU=Users,DC=Contoso,DC=com
Is there another way to show their Name and/or SamAccountname?
$Groups =
#"
GroupNames;
Group_A
"# | ConvertFrom-Csv -Delimiter ';'
$ADGroups =
Foreach ($Group in $Groups){
Get-ADGroup $Group.GroupNames -Server contoso.com -Properties Members }
$ADGroups.Members
As the other helpful answers show, if you want to play safe, you can use Get-ADGroupMember to get the group membership, this would also be useful because you would be able to distinguish the ObjectClass of each member.
You could also do string manipulation over the elements (distinguishedName) of the member attribute of the AD Group by following this Q&A.
If the members of the group are on different Domains, this should work however it would be quite slow most likely.
foreach($group in $groups) {
$membership = Get-ADGroup $Group -Properties Member
$membership.Member | Group-Object { ($_ -split '(?=DC=)',2)[1] } |
ForEach-Object {
[adsi]$ldap = 'LDAP://{0}' -f $_.Name
[string]$domain = $ldap.Name
foreach($member in $_.Group) {
$obj = Get-ADObject $member -Server $domain
[pscustomobject]#{
MemberOf = $membership.Name
Domain = $domain
SamAccountName = $obj.SamAccountName
ObjectClass = $obj.ObjectClass
}
}
}
}
Get-ADGroupMember has two parameters you can use for that. samaccountname, and name.
Simply do the following:
Get-ADGroupMember -identity $ADGroup | select-object SamAccountName, Name
Or in your code snippet:
Foreach ($group in $groups) {
Get-AdGroup -identity $group | select-object Samaccountname, Name }
Of course you could add:
Get-AdGroup -identity $group | select-object Samaccountname, Name | export-csv C:\mypath\report.csv
You could run a query against the returned values using Get-ADObject since it accepts DistinguishedNames as a value and isn't limited by object class:
foreach ($Group in $Groups)
{
(Get-ADGroup $Group.GroupNames -Server contoso.com -Properties Members).Members |
ForEach-Object -Process {
Get-ADObject -Identity $_ -Properties DisplayName | Select-Object -Property DisplayName
}
}
...or, you can split the results at the desired entry:
foreach ($Group in $Groups)
{
(Get-ADGroup $Group.GroupNames -Server contoso.com -Properties Members).Members |
ForEach-Object -Process {
$_.Split(',',2).Split("=")[1]
}
}
Disclaimer: I don't have the AD Module installed on my system so I can't confirm if this is all that is needed.
The easiest way would be to expand the members property and in Get-ADGroup and then pipe it to Get-ADUser
$adUsers = Foreach ($Group in $Groups) {
Get-ADGroup $Group.GroupNames -Server contoso.com -Properties Members | Select-Object -ExpandProperty Members | Get-aduser
}

Get Ad group members and properties from wildcard groups then export to csv

I'm trying to export group members and their properties from groups with names starting with XX so the output is
output
With the code i wrote i can get all the users and properties but i would also like to export the group in front of each member
$Groups = Get-ADGroup -filter {name -like "Group*"} | Select-Object Name
foreach ($Group in $Groups)
{
$Members = Get-ADGroupMember -Identity $($Group.name) -Recursive | Select-Object name
foreach ($Member in $Members )
{ Get-ADUser -Identity $($Member.name) -Properties * | Select-object * | export-csv
}
}
Thank you in advance, guys.
Store the user object in a variable, add the group as a new member of that object and pipe it to Export-CSV.
$Groups = Get-ADGroup -filter {name -like "Group*"} | Select-Object Name
foreach ($Group in $Groups) {
$Members = Get-ADGroupMember -Identity $($Group.name) -Recursive | Select-Object name
foreach ($Member in $Members) {
$UserObject = Get-ADUser -Identity $($Member.name) -Properties * | Select-Object *
$UserObject.Group = $($Group.name)
$UserObject | Export-CSV
}
}

Pull Ad groups and count the users inside each group

I want to pull all AD groups in an OU and then print out each group and the user count thats in that group. The way I currently have it, it just counts how do I get the group name with the count of members?
Import-Module ActiveDirectory
$groups = (Get-ADGroup -Filter {GroupCategory -eq 'security'} -SearchBase 'Path to OU' | select SamAccountName).samaccountname
foreach ($group in $groups){
(Get-ADGroup -Identity $group | select name).count
}
Use members attribute, and count that one.
Import-Module ActiveDirectory
$groups = Get-ADGroup -Filter {GroupCategory -eq 'security'} -SearchBase 'Path to OU' -Properties *
foreach ($group in $groups) {$group.members.count}
Or, since you want both,
$groups | select name, {$_.members.count}

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

Export CSV of AD SamAccountNames and Groups for every user in specific OU

I found a similar question here, but it doesn't quite fit my need and I am having trouble tweaking it to do so.
I need to create a .csv file of all users in a specific OU along with what their AD group membership is in the following format:
User, Group (This is a Header)
User1, Group1
User1, Group2
User1, Group3
User2, Group1
User3, Group1
User4, Group1
User4, Group2
I think this script gets me most of the way there:
$Users = Get-ADGroup -SearchBase "OU=OrgUnit1,OU=OrgUnit2,OU=OrgUnit3,DC=XXX,DC=LOCAL" -Filter * `
| Get-ADGroupMember -Recursive `
| ForEach-Object { Get-ADUser $_ –Properties MemberOf | Select SamAccountName, MemberOf; } `
| Sort-Object SamAccountName
| export-csv C:\Messaging\PowerShell\ADUsers\Test1.csv
The problem with this is two fold.
I want to search on OU=OrgUnit1 without having to search on the full distinguished name, because the sub OU's aren't always the same.
The .csv output has the full distinguished name of the AD Group and I need just the Name of the group with no qualifiers
Use Get-ADOrganizationalUnit to get the OU you want to search:
$ou = Get-ADOrganizationalUnit -Filter "Name -eq 'OrgUnit1'"
Get-ADGroup -Filter * -SearchBase $ou | ...
The memberOf property is a list of groups (or rather their distinguished names). To get the output you want you need to unroll and resolve the group names and create new custom objects with the desired properties:
... | ForEach-Object {
$account = $_.SamAccountName
$_.MemberOf | Get-ADGroup | ForEach-Object {
New-Object -Type PSCustomObject -Property #{
SamAccountName = $account
Group = $_.Name
}
}
} | ...
Also, there's no point in assigning pipeline output to a variable ($Users) if at the end of that pipeline you export the output to a file.
Modified code:
$ou = Get-ADOrganizationalUnit -Filter "Name -eq 'OrgUnit1'"
Get-ADGroup -Filter * -SearchBase $ou |
Get-ADGroupMember -Recursive |
ForEach-Object { Get-ADUser $_ -Properties MemberOf; } |
Sort-Object SamAccountName |
ForEach-Object {
$account = $_.SamAccountName
$_.MemberOf | Get-ADGroup | ForEach-Object {
New-Object -Type PSCustomObject -Property #{
SamAccountName = $account
Group = $_.Name
}
}
} | Export-Csv 'C:\Messaging\PowerShell\ADUsers\Test1.csv'
You don't need this much of code to write. User below code in PowerShell to export all AD user.
Something like this:
Import-Module ActiveDirectory
Get-ADUser -Filter * -Properties * | export-csv c:\ADusers.csv
If you have a big AD, that might take a while though.