locked
Get-Aduser -Filter Option -notlike does not work RRS feed

  • Question

  • Hello All;
    Attempting to use Get-Aduser to find entries in Active directory that are not in a text file.  The -like option appears to work but cannot seem to get the -notlike.  When I use the -nolike option the entries in the text file appear as part of the output file. Using the -like option the powershell works. 

    Here is the contents of the text file 
    svcXXSQL001Agent
    svcXXSQL001DBEng
    svcXXSQL001Int
    svcXXSQLUAT501DBEng
    svcxxapp211

    Here is my existing code:
    $server=get-content C:\temp\test.txt
     foreach ($name in $server) {
     Get-ADUser -SearchBase “OU=ServiceAccts,DC=nlong,DC=com”  -Filter "name -notlike '$name'"  | sort | Where-Object {$_.Name -like "svcxxsql*"} | Select-Object Name | Out-File -FilePath C:\temp\foo.txt
      
       }

    Thanks for the input

    Norm
    Thursday, February 15, 2018 6:24 PM

All replies

  • Like operations require the wildcard character.

     Get-ADUser -SearchBase “OU=ServiceAccts,DC=nlong,DC=com”  -Filter "name -notlike '$name*"

    . Always Use Sort at the end of the pipeline, here just before Out-File.


    Regards kvprasoon

    Thursday, February 15, 2018 6:59 PM
  • You query AD multiple times.  You rewriting your foo.txt multiple times.  You should not use like/notlike operator when u wanna filter out specific accounts.  You ...    

    $Exclude = @( 'svcXXSQL001Agent', 'svcXXSQL001DBEng', 'svcXXSQL001Int' ) $filter = ($Exclude | foreach {'(Name -ne ' + "'$_')"}) -join ' -and ' $ou = "OU=ServiceAccts,DC=nlong,DC=com"

    Get-ADUser -Filter $filter -SearchBase $ou | ?{$_.name -like "svcxxsql*"}



    Thursday, February 15, 2018 9:00 PM