locked
Find samaccountnames containing spaces RRS feed

  • Question

  • I am trying to search for SamAccountNames that contain spaces anywhere in the name, but the following code doesn't work. Is this as simple as I think it is, or is it more complicated?

    get-aduser -filter "samaccountname -like ' '"


    Wednesday, March 12, 2014 2:03 PM

Answers

  • Try samaccountname -like '* *'

    The where/match option should also work, but is less efficient than using filter in the initial get-aduser command.

    • Marked as answer by KentYeabower Wednesday, March 12, 2014 2:25 PM
    Wednesday, March 12, 2014 2:18 PM

All replies

  • hmm.. not verified but..

    $regex = "\s"
    get-aduser -filter * |?{$_.samaccountname -match $regex}

    Wednesday, March 12, 2014 2:11 PM
  • Try samaccountname -like '* *'

    The where/match option should also work, but is less efficient than using filter in the initial get-aduser command.

    • Marked as answer by KentYeabower Wednesday, March 12, 2014 2:25 PM
    Wednesday, March 12, 2014 2:18 PM
  • Rhysaroni,

    That worked great! Thanks!

    Mekac,

    Your script was running for a while so I just stopped it, but thanks for the suggestion!

    Wednesday, March 12, 2014 2:25 PM
  • Can also use curly braces to make the filter easier to read:

    get-aduser -Filter {samaccountname -like "* *"} 


    Friday, December 21, 2018 6:06 AM
  • The funny thing is -filter is a string parameter, even though one of the -filter examples here uses a script block.  https://docs.microsoft.com/en-us/powershell/module/addsadministration/get-aduser?view=win10-ps  It will just get cast from a script block to a string.
    • Edited by JS2010 Friday, December 21, 2018 2:58 PM
    Friday, December 21, 2018 2:58 PM