Script to list AD groups and inherited AD groups for a User

Hello,

I am looking from this beginning of the script to use the result with a Foreach to list the Inherited groups:

Param (
[String]$Name =“”
)
While (!$Name)
{
$Name = Read-Host “Nom du compte”
}

$Name = Get-ADUser $Name -Properties *

$Name.Name
$Name.Memberof

The goal is to have the list of groups to which a user belongs but also the list of groups he inherited.

If anyone has any idea how I could do this I’m all ears…

Thanks in advance to anyone who can help me

Mike,
Welcome to the forum. :wave:t4:

Please … When you post code, sample data, console output or error messages format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

You need to query all groups the user belongs to recursivly for their members where the objectClass is equal to group

But … as always … most of the time you’re not the first one with a given task. Please use your favorite internet search engine to search for examples you can adapt to your particular needs. You don’t need to re-invnet the wheel again. :wink:

Hi Olaf and thanks for your answer,

I have been browsing the net for two long days looking for information but unfortunately I have not found anything that has allowed me to move forward.

Very sorry i take the advice !

If I take your subject, add “PowerSehll” to it and search for that I actually find enough promissing results:
https://www.google.com/search?q=powershell+list+AD+groups+and+inherited+AD+groups+for+a+User

Hi !

Thank you for your answer it allowed me to move forward. Here’s my script right now:

Param ([String]$Name =“”)
While (!$Name)
{
$Name = Read-Host “Nom du compte”
}
$Name = Get-ADUser $Name -Properties *
#$Name.Name
#$Name.Memberof
#Get all recursive groups a user belongs.
Function Get-ADUserNestedGroups
{
Param
(
[string]$DistinguishedName,
[array]$Groups = @(),
$AD = @()
)
If (!$AD) {$AD = Get-ADObject -Filter * -Properties *}
#Get the AD object, and get group membership.
#$ADObject = Get-ADObject -Filter “DistinguishedName -eq ‘$DistinguishedName’” -Properties memberOf, DistinguishedName
$ADObject = $AD | Where-Object DistinguishedName -eq $DistinguishedName
#If object exists.
If($ADObject)
{
#Enummurate through each of the groups.
Write-Host ($ADObject.DistinguishedName + “-”)
Foreach($GroupDistinguishedName in $ADObject.memberOf)
{
#Get member of groups from the enummerated group.
#$CurrentGroup = Get-ADObject -Filter “DistinguishedName -eq ‘$GroupDistinguishedName’” -Properties memberOf, DistinguishedName
$CurrentGroup = $AD | Where-Object DistinguishedName -eq $GroupDistinguishedName
#Check if the group is already in the array.
If(($Groups | Where-Object {$_.DistinguishedName -eq $GroupDistinguishedName}).Count -eq 0)
{
#Add group to array.
$Groups += $CurrentGroup
#Get recursive groups.
$Groups = Get-ADUserNestedGroups -DistinguishedName $GroupDistinguishedName -Groups $Groups -AD $AD
}
}
}
#Return groups.
Return $Groups;
}
#Get all groups.
$Groups = Get-ADUserNestedGroups -DistinguishedName (Get-ADUser -Identity $name).DistinguishedName;
#Output all groups.
$Groups | Select-Object Name | Sort-Object -Property Name; >

What do you think about it?

?? :man_shrugging:t4:

Does it do what you need?

Indeed the script comes out to me well the desired results, on the other hand I want to make them stand out in text file and can be simplify the script itself.

You still did not format your code as code. You can update your existing post and fix it.

What text file? In the code you share I cannot see any reference to a text file. For structured data I’d recommend to use CSV files anyway.

hello, I fixed the script could you tell me if for you it is more readable?

I’m working to structured a CSV files !

I appreciate you trying, but still isn’t quite right. It looks like you “quoted” your code versus using the preformatted option </>

I also hope your code is indented as what you’re showing here is not. Did you also have some sample CSV lines?