Powershell Members of a Group

Every now and again there comes a need to get the members of a group and manipulate that data in some fashion.

Now there are many ways to skin this cat and work with this data from VBscript, to utilizing DSquery to Quest Powershell CMDLETS to just raw powershell cmdlets.

Rather than spend time writing a new script every time you work at a different place or having to change constants in a script I spent some time writing a quick powershell script that accepts some variable information an outputs a CSV, or TXT file (your choice) with the user’s SAMaccountName combined with first and last name for all members of that group.

You can copy and download a version of the PS1 Script from Here to make sure there are no white space errors.


##########################################################################
# Powershell Written by Jordan Benzing                                   #
# Last edited on 10/30/2015                                              #
# Version 1.0.0                                                          #
# Disclaimer: This is provided as is and gaurantees no support           #
# Just because this script works in my environment does not mean it will #
# always work in your environment. Please perform appropriate testing for#
# your environment.                                                      #
# Any updates will be hosted at problemresolution.wordpress.com          #
#                                                                        #
##########################################################################

$GroupName = Read-Host "Enter the name of your group here"
$DomainName = Read-Host "Enter the Domain you are querying here"
$SaveFile = Read-Host "Where would you like to save the output? Example: C:\users\Username\Desktop\test.csv"
$MembersofGroup = Get-ADGroupMember -Identity $GroupName
$AllUsers = {$TheUsers}.Invoke()
ForEach ($Member in $MembersofGroup){
      $CurrentUser = $Member.SamAccountName
      $CurrentUser = Get-ADUSER -Server $DomainName -Identity $CurrentUser
      $AddedUser = $CurrentUser.SamAccountName + ' , ' + $CurrentUser.GivenName + " " +          $CurrentUser.Surname
      $AllUsers.Add($AddedUser)
}
$AllUsers | Out-File $SaveFile
 

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: