I have a PowerShell script that I’ve been using to spit out a CSV of all users in a specific OU, but I want to exclude users that are a member of a specific security group. Basically, all the users exist in the same OU otherwise I could choose which OU to pull from.
The only unique factor is the group they are a member of. What could I use to exclude these users?
Would adding this to the ‘get-aduser’ line work?
groupattribute -notcontains "<groupname>"
Here is a section of the script
$csvdata = get-aduser -filter {enabled -eq $true -and EmailAddress -like "*@domain*" -and givenname -like "*" -and surname -like "*"} -searchbase "OU=#someOU,DC=#companyname#,DC=com" -properties mail,country,department | foreach{ new-object psobject -property @{ email = $_.mail first_name = $_.givenname last_name = $_.surname group = $_.country department = $_.department } } | select email,first_name,last_name,group,department $payload = "email,first_name,last_name,group,department`n" # filter out problematic data $csvdata = $csvdata | Where-Object { ($_.email -match '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b') } | Sort-Object * -Unique $csvdata | ForEach-Object { $payload = [string]::Concat($payload, $_.email, ",") $payload = [string]::Concat($payload, $_.first_name, ",") $payload = [string]::Concat($payload, $_.last_name, ",") $payload = [string]::Concat($payload, $_.group, ",") # $payload = [string]::Concat($payload, $_.mobile, ",") # $payload = [string]::Concat($payload, $_.alternate_email, ",") # $payload = [string]::Concat($payload, $_.alternate_mobile, ",") # $payload = [string]::Concat($payload, $_.sms_enabled, ",") $payload = [string]::Concat($payload, $_.department, "`n") }