getADGroupMembershipWithProperties.ps1

<#
<#PSScriptInfo
 
.VERSION 1.1
 
.GUID 9204ded2-14ab-4b64-b0ec-ae98134ba87a
 
.AUTHOR M. Hashemi
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
#>


<#
 
.DESCRIPTION
 Get the group membership of a given AD group and return a user-specified list of attributes to a CSV file.
.PARAMETER GroupName
    Required. This parameter represents the name of the group to investigate.
.PARAMETER SearchOU
    Required. This parameter represents the OU containing the group to investigate.
.PARAMETER ParentTree
    This parameter represents the part of the group DN that is not covered in the GroupName and SearchOU parameters. ParentTree is employed so the user doesn't have to type the entire DN if it falls under the default value.
.PARAMETER OutputProperties
    Default value = Name,EmailAddress. This parameter represents a comma-spearated list of AD attributes to return.
.PARAMETER OutputFilePath
    Default value = [Environment]::GetFolderPath("Desktop"). This parameter represents the path to store the output file.
.PARAMETER OutputFileName
    Default value = $GroupName + '_GroupMembership.csv'. This parameter represents the name of the output file.
.EXAMPLE
    .\getADGroupMembershipWithProperties-Parameterized.ps1 -GroupName users -SearchOU 'Accounting' -ParentTree 'OU=USUsers,DC=domain,DC=local'
    This example will output the name and e-mail address of the members of the "users" group in the "OU=accounting,OU=USUsers,DC=domain,DC=local" OU. The results will be output to the user's desktop, in a file called users_GroupMembership.csv.
.EXAMPLE
    .\getADGroupMembershipWithProperties-Parameterized.ps1 -GroupName users -SearchOU 'Accounting' -OutputProperties name,telephoneNumber
    This example will output the name and telephone number of the members of the "users" group in the "OU=accounting,OU=USUsers,DC=domain,DC=local" OU. The results will be output to the user's desktop, in a file called users_GroupMembership.csv.
.EXAMPLE
    .\getADGroupMembershipWithProperties-Parameterized.ps1 -GroupName users -SearchOU 'Accounting' -OutputFilePath c:\
    This example will output the name and e-mail address of the members of the "users" group in the "OU=accounting,OU=USUsers,DC=domain,DC=local" OU. The results will be output to the C:\ drive, in a file called users_GroupMembership.csv.
.EXAMPLE
    .\getADGroupMembershipWithProperties-Parameterized.ps1 -GroupName users -SearchOU 'Accounting' -OutputFileName file.csv
    This example will output the name and e-mail address of the members of the "users" group in the "OU=accounting,OU=USUsers,DC=domain,DC=local" OU. The results will be output to the user's desktop, in a file called file.csv.
#>
 
[CmdletBinding()]
param(
    [Parameter(Mandatory=$True)]
    [string]$GroupName,

    [Parameter(Mandatory=$True)]
    [string]$SearchOU,
    
    [Parameter(Mandatory=$True)]
    [string]$ParentTree,

    [string]$OutputProperties = 'Name,EmailAddress',
        
    [string]$OutputFilePath = [Environment]::GetFolderPath("Desktop"),

    [string]$OutputFileName = $GroupName + '_GroupMembership.csv'
)
#Requires -Version 2.0

Import-Module ActiveDirectory
If ((Get-Module ActiveDirectory -ErrorAction SilentlyContinue) –eq $null) {
    Write-Host "This script requires the Powershell Module: 'ActiveDirectory'. Please make sure you've got the correct tools installed." -ForegroundColor Red
    Exit   
}
Else {
    $groupDN = "CN=$GroupName,OU=$SearchOU,$ParentTree"

    ForEach ($member in (Get-ADGroupMember $groupDN)) { 
        Get-ADUser -Filter {Name -eq $member.Name} -Properties * | Select $OutputProperties.Split(",") | Export-Csv -Path $OutputFilePath'\'$OutputFileName -NoTypeInformation -Append
    }
}