Script to Output ADUser, Group, and Enabled to CSV

Hi all!

I'm trying to put together a script that grabs all enabled users from AD and the respective groups of which they are a member. Finally, I would like to have the output go to a CSV.

I found this script which appears to do what I am looking for, but the output is non-existent; the file is blank. I guess the good thing is there are no errors.

$DateTime = Get-Date -f "yyyy-MM"

Get-ADGroup -filter "enabled -eq 'true'"| Foreach-Object{
$Grp = $_ | Select Name, GroupCategory, Groupscope
$users = Get-ADGroupMember $_ |get-aduser -filter "enabled -eq 'true'" |select Name

New-Object -TypeName PSObject -Property @{
member = $Users.Name
GroupName = $
Groupscope = $grp.GroupScope
GRPCategory = $grp.GroupCategory

} |select Member,Groupname,Groupscope,Grpcategory| Export-Csv "C:\Scripts\Output\AD_Groups_and_Users $DateTime.csv" -NoTypeInformation

Can someone please help me?

No Data
  • You will probably want to change the output at the bottom, this creates individual CSV files for each account (all accounts, not just users), containing their AD groups. Great for small AD environments or if you change $GrabEnabled to something like "Read-Host "Account Name" (which is how I wrote the script originally). You might be able to use something like add-content to get it all in one file - but I'm relatively new to powershell in my opinion, so there may be better ways.

    #Variable that grabs all enabled accounts
    $GrabEnabled = get-aduser -filter 'enabled -eq $true'
    #For each user that was grabbed
    foreach ($user in $GrabEnabled){
    #Get their groups (except Domain Users)
    $ADgroups = Get-ADPrincipalGroupMembership -Identity $user | where {$_.Name -ne "Domain Users"}
    #Filter the groups to the group names only
    $ADOut = $ADgroups | select name
    #Output group memberships to file, with their username as the name of the file
    $ADOut | export-csv $("c:\scripts\output\" + $user.SamAccountName + ".csv")

No Data