Handy PowerShell Script – AD Group Membership

I had a request to write a quick script to output the Group Membership  for Active Directory users. The following is what I came up with.

##
## Version 1.1
## Powershell Script that collects what groups users login account is part of and stores it within a CSV.
##
## Author: Josh Ancel
## Date Written: 5/2/2013
##

import-module activedirectory

## Reads where the user would like to store the .csv file on ths system.
$fileLocation = Read-Host “Enter the path and file name where you would like to store the information. Example: C:\Temp\UserGroupMembership.csv :”

Do
{
## This is the account name. This needs to be be the same syntax as what the AD user logs into any domain. Example: Josh.Ancel
$strUserName = Read-Host “Enter the Login Name:”
## Adds a line in the output csv file that list the username before the group data is listed.
Add-Content $fileLocation $strUserName
## Gets Group Membership Data for the requested user.
$memData = (GET-ADUSER -Identity $strUserName -Properties MemberOf | Select-Object MemberOf).MemberOf
## Adds the Group Membership data to the output csv file.
Add-Content $fileLocation $memData

## This contiditon lets the program know if it should end or add more data.
$conDition = Read-Host “Are there more accounts you would like to enter? (Y)es or (N)o”
##Clears out the memberdata before proceeding with the next user. This prevents false data being entered when an error occurs with the username.
$memData = “”
}
until ($conDition -eq “N”)

## EOF

Personally I like to output it to a .CSV and to edit in Excel, but you can define .txt if you like the format better.

I hope others find it useful. Good Luck!

Advertisements

~ by Josh on May 2, 2013.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: