Solved

Powershell & Ad Users & Groups

Posted on 2014-10-10
9
232 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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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
 
LVL 12

Expert Comment

by:Vaseem Mohammed
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Expert Comment

by:Joshua Grantom
Comment Utility
You can add a filter to get-aduser

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

This will only return enabled user accounts.
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now