Solved

Powershell & Ad Users & Groups

Posted on 2014-10-10
9
248 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 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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