Script to export AD users

Hi,
I am not an expert in Powershell scripting

I need to export to a CSV, Users from an OU and include groups they are member of AND the groups in groups (nested groups). Also it should be good to include a column for disabled account into the CSV.

Thanks you very much for your help

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

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Regardless of that … what you asked for is a very common task. It has been asked a lot of times already and it has been answered a lot of times already. You don’t need to re-invent the wheel again. Please use your prefered internet search engine to look for code examples you can adapt to your particular requirements.

Hi
I understand the role of this forum

So, I already have this script that give me all the users and groups but i also need groups in groups

Get-ADUser -Filter * -SearchBase “OU=Users,OU=Test,DC=com” -Properties memberOf |
Foreach-Object{
# $_ represents a user object
$var = [PSCustomObject]@{
SID = $.SamAccountName
Name = $
.Name
Group = “”
}
if ($.memberOf){
$groups = @()
$
.memberOf |
ForEach-Object{
$groups += (Get-ADGroup $_).samaccountname
}
$var.Group = $groups -join ‘;’
$var
}

 } | Export-Csv -Path C:\powershell\Lac-UsersWithGroups.csv -NoTypeInformation 

What can i add to reach my goal?
Thanks in advance

As you may noticed … the forum software messed up you code a little bit because you did not format it as code.

So 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:
.

In the vast majority of the cases you’re not the very first one with a given task. You don’t need to re-invent the wheel again.

Use your prefered internet search enginge to search for code examples you can adapt to your particular needs.

Something like this should get you started:

https://www.google.com/search?q=Powershell+query+user+groupmembership+nested+groups