$group = Read-Host "Enter the group name" Add-Type -AssemblyName System.Windows.Forms $FileBrowser = New-Object System.Windows.Forms.SaveFileDialog -Property @{ InitialDirectory = [Environment]::GetFolderPath('Desktop') FileName = $group Filter = 'CSV (*.csv)|*.csv' } $null = $FileBrowser.ShowDialog() $list = @() $users = Get-ADGroupMember $group -Recursive $users2 = Get-ADGroupMember $group ForEach ($user in $users) { $nested = "True" ForEach ($user2 in $users2) { If ($user.SID -eq $user2.SID) {$nested = "False"} } If ($user.objectClass.ToString() = "user") { Try { $aduser = get-aduser $user -Properties Enabled, EmailAddress $email = $aduser.EmailAddress $enabled = $aduser.Enabled.ToString() } Catch { $email = "" $enabled = "" } $Properties = [ordered]@{'Name'=$aduser.Name;'Email'=$email;'SamAccountName'=$user.SamAccountName;'Nested'=$nested;'Enabled'=$enabled;'Type'="user"} $list += New-Object -TypeName PSObject -Property $Properties } Else { $Properties = [ordered]@{'Name'=$user.Name;'Email'="";'SamAccountName'=$user.SamAccountName;'Nested'=$nested;'Enabled'="";'Type'=$user.objectClass.ToString()} $list += New-Object -TypeName PSObject -Property $Properties } } ForEach ($user2 in $users2) { If ($user2.objectClass -eq "group") { $Properties = [ordered]@{'Name'=$user2.Name;'Email'="";'SamAccountName'=$user2.SamAccountName;'Nested'="False";'Enabled'="";'Type'="group"} $list += New-Object -TypeName PSObject -Property $Properties } } $list | Sort Name | Export-Csv $FileBrowser.FileName -NoTypeInformation