none
Powershell combine filter for select adgroupmembers RRS feed

  • General discussion

  • Hi, I'm trying to combine a double filter to get a selection of users from a securitygroup. In the script below I want to ad a filter like  I'm using in Objectscipt but I can't find the right format. I want the users with no initials to be excluded in the result.

    How can I fix this?

    This is the part from the objectscript:

    ForEach-Object {Get-ADUser -Filter * -Properties * | Where {($_.Initials -ne $null)} 

    This is my script for exporting nested users:

     
    Import-Module ActiveDirectory
    $securitygroup=Read-Host "Please enter the securitygroup to export"
    $path =Read-Host "Please enter the existing (local) export path (like c:\temp)"
    Get-ADGroupMember -Recursive $securitygroup | 
    #----------------------------------------------------------------------------------------
    ForEach {Get-ADUser -filter {samaccountname -eq $_.SamAccountName} -Properties Department, info, description, Office, MobilePhone, ipPhone, company, initials, GivenName, EmailAddress, Surname, SamAccountName
    }  | Sort-Object company,displayName | 
    #----------------------------------------------------------------------------------------
    #Format-Table Department, info, description, Office, MobilePhone, ipPhone, company, initials, GivenName, EmailAddress, Surname, SamAccountName -AutoSize |
    
    select Department, info, description, Office, MobilePhone, ipPhone, company, initials, GivenName, EmailAddress, Surname, SamAccountName | export-CSV -path $path\"$securitygroup.csv" -Encoding UTF8 -NoTypeInformation

    • Changed type Bill_Stewart Tuesday, April 14, 2020 1:35 PM
    • Moved by Bill_Stewart Tuesday, April 14, 2020 1:36 PM Abandoned
    Wednesday, September 18, 2019 12:14 PM

All replies

  • Usually I recommend using an LDAP filter string:


    Get-ADUser -LDAPFilter "(!initials=*)"

    This will return all AD users in the current domain where the initials attribute is empty.

    Remove the ! character to say "initials attribute is not empty".


    -- Bill Stewart [Bill_Stewart]

    • Edited by Bill_Stewart Wednesday, September 18, 2019 2:05 PM
    Wednesday, September 18, 2019 2:04 PM