Get-ADUser Filter query

Roger Roger 7,181 Reputation points
2024-08-20T05:23:27.4833333+00:00

Hi All,

In Active Directory, I have the office and country attributes in the following format:

Office=Remote, FL, USA Country=United States of America 
Office=FL, USA Country=United States of America

I want to pull all users who have 'Remote' in their office location and 'United States of America' as their country. For example, if any user has an office location like 'Remote, UT,' they should also be included. Is the following query correct? (It should pull combination of 'Remote' and 'United States of America'):

(Office -like 'Remote*') -and (co -like '*United States of America*')

I also want to pull all users who have 'FL' in their office location and 'United States of America' as their country. However, when I use the following query, it also pulls users with 'Remote' in their office location, i dont want remote users.i am just looking for FL user not Remote, FL users please guide me.

(Office -like 'FL*') -and (co -like '*United States of America*')

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2024-08-20T19:23:24.5466667+00:00

    Do you want both conditions in the same pass, or in two separate passes?

    This should work if you stick with using the "Country" property.

    # Remote users in US
    get-aduser  -Filter * -properties Office,Country|
        Where-Object {$_.Office -like "Remote,*" -And $_.country -eq "US"} |
            Select-Object Name, Office, Country
    
    # Users in the state of Florida, US
    get-aduser  -Filter * -properties Office,Country|
        Where-Object {$_.Office -match "^FL," -And $_.country -eq "US"} |
            Select-Object Name, Office, Country
    

    The "co" property is just "United States", isn't it?

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Roger Roger 7,181 Reputation points
    2024-08-20T22:28:20.8666667+00:00

    Remote users in US is working perfectly fine.

    Users in the state of Florida, US is not working i tried the below combinations

    $query ="(Office -match '^FL,' -and country -eq 'United States of America')" 
    Get-ADUser -Filter $query -Properties Name,Office,Country|Select Name,Office,Country
    
    $query ="(Office -like '^FL,' -and country -like 'United States of America')" 
    Get-ADUser -Filter $query -Properties Name,Office,Country|Select Name,Office,Country
    
    $query ="((Office -like 'FL,') -and (country -like 'United States of America'))" 
    Get-ADUser -Filter $query -Properties Name,Office,Country|Select Name,Office,Country
    
    $query ="((Office -like '^FL,') -and (country -like 'United States of America'))"
    Get-ADUser -Filter $query -Properties Name,Office,Country|Select Name,Office,Country
    
    
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.