PS Search filter not recognized Get-AdUser

Newbie 0 Reputation points
2023-04-04T16:56:40.1933333+00:00

I am getting an error with the script, anyone see any issues obvious?

Error Message:

WARNING: Failed to update 
Get-ADUser : The search filter cannot be recognized
At H:\updatelocale.ps1:4 char:15
+ ...   $ADUser = Get-ADUser -Filter "displayName -eq '$($user.displayName) ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
IMPORT-MODULE ActiveDirectory
Import-Csv -Delimiter ";" -Path H:\test.csv | Foreach {
    # Find user
    $ADUser = Get-ADUser -Filter "displayName -eq '$($user.displayName)'" -Properties msExchUsageLocation

    if ($ADUser){
        Set-ADUser -Identity $ADUser -msExchUsageLocation $user.msExchUsageLocation
    }else{
        Write-Warning ("Failed to update " + $($_.displayName))
    }
}

CSV Entries:
displayName,msExchUsageLocation
john doe,US
Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
1,999 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,462 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,329 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. H Risbud 251 Reputation points
    2023-04-04T17:40:51.25+00:00

    Try

    $ADUser = Get-ADUser -Filter {displayName -eq '$($user.displayName)'} -Properties msExchUsageLocation or $ADUser = Get-AdUser -Filter * -Properties * | Where {$_.DisplayName -eq '$($user.displayName)'}


  2. Rich Matheisen 45,906 Reputation points
    2023-04-04T18:04:00.8766667+00:00

    The Import-CSV specifies that the delimiter is a semicolon, but the CSV that you posted uses a comma as the delimiter. Either change the Import-CSV by removing the '-Delimiter ";"' altogether, or changing it to '-Delimiter ","'.


  3. H Risbud 251 Reputation points
    2023-04-04T22:30:21.41+00:00

    This code will solve this issue

    Import-Module ActiveDirectory
    Import-Csv -Delimiter "," -Path H:\test.csv | Foreach {
        # Find user
        $ADUser = Get-ADUser -Filter {displayName -eq '$($_.displayName)'} -Properties msExchUsageLocation
    
        if ($ADUser){
            Set-ADUser -Identity $ADUser -msExchUsageLocation $user.msExchUsageLocation
        }else{
            Write-Warning ("Failed to update " + $($_.displayName))
        }
    }