# Import the required module try { Import-Module ActiveDirectory -ErrorAction Stop } catch { Write-Error "Failed to import ActiveDirectory module. Error: $_" return } # Get all AD groups try { $groups = Get-ADGroup -Filter * -ErrorAction Stop } catch { Write-Error "Failed to get AD groups. Error: $_" return } # Create an empty array to hold the data $results = @() # Loop over each group foreach ($group in $groups) { # Skip the "IIS_IUSRS" group if ($group.Name -eq "IIS_IUSRS") { continue } # Get the members of the current group try { $members = Get-ADGroupMember -Identity $group -ErrorAction Stop | Where-Object {$_.objectClass -eq 'user'} } catch { Write-Error "Failed to get members for group $($group.Name). Error: $_" continue } # Check if the group has no members if ($members.Count -eq 0) { # Create a new object to represent the group with no members $emptyGroup = [PSCustomObject]@{ "Group Name" = $group.Name } # Add the empty group object to the results array $results += $emptyGroup } } # Set the output directory and filename $outputDir = "C:\ADAudit" $outputFile = Join-Path -Path $outputDir -ChildPath "T. BlankADGroups.csv" # Create the directory if it doesn't exist if (!(Test-Path -Path $outputDir)) { New-Item -ItemType Directory -Path $outputDir | Out-Null } # Export the results to a CSV file try { $results | Export-Csv -Path $outputFile -NoTypeInformation -ErrorAction Stop } catch { Write-Error "Failed to export results to CSV. Error: $_" }