?
Solved

Powershell & Ad Users & Groups

Posted on 2014-10-10
9
Medium Priority
?
250 Views
Last Modified: 2014-10-14
Need to have a Powershell Script that will pull users and the group memberships they belong to.
0
Comment
Question by:rjordanbots
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 40374246
To collect the group membership of all users.. Try following code.. You need to have at least Win 2008 R2 Domain controllers to use the ActiveDirectory module commands.
Import-Module ActiveDirectory
Get-ADUser -Filter * | foreach-object {
$User = $_.Name 
 Get-ADPrincipalGroupMembership $_.SamAccountName | Select @{N="User";e={$User}},@{N="User";e={$_.Name}}
}| Export-Csv c:\temp\GroupMembership.csv -nti

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40374248
Get-ADUser -SearchBase "OU=accounts,dc=test2k8,dc=local" -filter * -properties samaccountname,memberof | select samaccountname, memberof | % {"$($_.samaccountname)`r`n$($_.memberof -join "`r`n")" | Out-File -FilePath "D:\Temp\$($_.samaccountname).txt"}
                                         

Similar  PAQ
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28454931.html
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40374255
There was a small typo in above code which I posted..
Import-Module ActiveDirectory
Get-ADUser -Filter * | foreach-object {
$User = $_.Name 
 Get-ADPrincipalGroupMembership $_.SamAccountName | Select @{N="User";e={$User}},@{N="Group";e={$_.Name}}
}| Export-Csv c:\temp\GroupMembership.csv -nti

Open in new window


You can also use -SearchBase parameter to search users from specific OU.
Get-ADUser -SearchBase "OU=contoso users,OU=SBG,DC=contoso,DC=com" -Filter * | foreach-object {...

Open in new window


Or read the user list from a text file (user.txt in this example)..
Import-Module ActiveDirectory
GC C:\Temp\user.txt | foreach-object {
$User = $_
 Get-ADPrincipalGroupMembership $User | Select @{N="User";e={$User}},@{N="Group";e={$_.Name}}
} | Export-Csv c:\temp\GroupMembership.csv -nti

Open in new window

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 12

Expert Comment

by:Vaseem Mohammed
ID: 40374772
0
 

Author Comment

by:rjordanbots
ID: 40380512
Is their a way to filter for only active accounts ?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40380533
You can add a filter to get-aduser

Get-ADUser -Filter 'enabled -eq $true'

This will only return enabled user accounts.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40380541
Slight mod on Subsun's script:

Import-Module ActiveDirectory
Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False}  -SearchBase "OU=ouname,dc=domain,dc=local" | foreach-object {
$User = $_.Name 
 Get-ADPrincipalGroupMembership $_.SamAccountName | Select @{N="User";e={$User}},@{N="Group";e={$_.Name}}
}| Export-Csv c:\temp\GroupMembership.csv -nti

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 40380544
If you can tell me the script which you are using? then I can modify it to output the enabled accounts..
0
 

Author Closing Comment

by:rjordanbots
ID: 40381216
Thank you for your help.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question