searching/filtering/where-object

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of michael glenn michael glenn 1 year, 10 months ago.

  • Author
    Posts
  • #26761
    Profile photo of michael glenn
    michael glenn
    Participant

    im trying to do a "get-childitem" search of directories that are all named with numbers.
    a 5 digit minimum consisting of digits from 0 to 9. directory example names like the following "13476" or "334787"
    tried the below.. among many other attempts, and cant seem to get the results I'd like.

    get-childitem c:\users | where-object {$_.name -like '*0-9*' }
    

    how can I accomplish this?

  • #26762
    Profile photo of michael glenn
    michael glenn
    Participant

    ok.. I got this to return correctly.. is this the best way to accomplish it?
    basically.. I want it to return directory names that contain any integer of any length.

    get-childitem c:\users | where-object {$_.name -match '[0-9]' }
    
  • #26766
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    Try something like this

    Get-ChildItem -Path c:\ | Where-Object {$_.BaseName -match '(^\d[0-9])'}
    

    I did test and it only gave me the folders that start with numbers.

  • #26776
    Profile photo of michael glenn
    michael glenn
    Participant

    awesome.. thanks for the suggestion.

  • #26806
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    use \d+ or \d{min.max} syntax if you want to get 1+ or min to max numbers length. btw, \d its the same as [0-9]

    "^\d+$" – give names that contains numbers only
    "^\d{3,6}$" – give names that contains numbers only but only from 3 to 6 characters long
    "^\d{5,}$" gives what you want – all digits, five or more

  • #26825
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You can actually filter with Get-ChildItem:

    Get-ChildItem -Path "C:\test\[0-9]*"
    

    It's referenced in this Powershell Tip:

    https://technet.microsoft.com/en-us/library/ff730956.aspx

  • #26958
    Profile photo of michael glenn
    michael glenn
    Participant

    thanks for the extra tips rob and max. its appreciated.
    now that ive got this aspect of the script working.. im running into the 256 character limit on directory length. always something.
    thanks again guys for all the help

You must be logged in to reply to this topic.