Get-ADUser Filter query

Roger Roger 5,801 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 Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,734 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,501 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,017 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,497 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,518 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,636 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 5,801 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.