Get AD Group Member details

Hi EE

I have the script below that will pull AD group member information from the groups entered in the txt file . Can someone help me modify it so it prompts me for the group name?


Import-Module Activedirectory
[array]$Members=$null
GC groups.txt | % {
$Group = Get-ADGroup $_  -ErrorAction SilentlyContinue
If ($Group){
$members += Get-ADGroup $Group.Name -Properties Members |
                Select-Object -ExpandProperty Members |
                Get-ADObject -properties Samaccountname |
                ?{$_.ObjectClass -eq "user"}  | Get-aduser -Properties * |
        Select @{L='GroupName';e={$Group.Name}},Name,Samaccountname,mail
 }
}
$Members | Select * | Export-Csv GroupDetails_$((Get-Date -Format MM-dd-yyyy)).csv -NoTypeInformation
LVL 2
MilesLoganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LearnctxEngineerCommented:
You haven't specified if you want direct members only or nested members? I've included options to achieve both. There are 2 ways to 2 this. First as a script parameter and secondly as a prompt from the script.

As a script using a named parameter
Execution: scriptname.ps1 -Identity GroupName -recursive (the recursive switch is optional)
Param (
	[Parameter(Mandatory=$true)]
	[string]$Identity = $null,
	[switch]$Recursive
)

if (-not (Get-Module ActiveDirectory))
{
	Import-Module Activedirectory
}

$Members = New-Object System.Collections.ArrayList
$Group = Get-ADGroup -Filter {sAMAccountName -eq $Identity}
If ($Group)
{
	if ($Recursive)
	{
		$Members = Get-ADGroupMember -Identity $Group -Recursive | Get-ADUser -Properties mail, displayname | select @{Name="GroupName";e={$Group}},Name,sAMAccountName,mail
	}
	else
	{
		$Members = Get-ADGroupMember -Identity $Group | Get-ADUser -Properties mail, displayname | select @{Name="GroupName";e={$Group}},Name,sAMAccountName,mail
	}
}

$Members | Export-Csv GroupDetails_$((Get-Date -Format MM-dd-yyyy)).csv -NoTypeInformation

Open in new window



As a script with a prompt for the group name (no recursive nested member option on this one)
if (-not (Get-Module ActiveDirectory))
{
	Import-Module Activedirectory
}

$Identity = Read-Host -Prompt "Enter the group name"

$Members = New-Object System.Collections.ArrayList
$Group = Get-ADGroup -Filter {sAMAccountName -eq $Identity}
If ($Group)
{
	$Members.AddRange(@(Get-ADGroupMember -Identity $Group | Get-ADUser -Properties mail, displayname | select @{Name="GroupName";e={$Group}},Name,sAMAccountName,mail))
}

$Members | Export-Csv GroupDetails_$((Get-Date -Format MM-dd-yyyy)).csv -NoTypeInformation

Open in new window



If you want to find all nested members and the group the user is nested in there's a bit more work to do especially if you want loop detection.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
Thank you Learnctx !!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.