searching/filtering/where-object

Welcome Forums General PowerShell Q&A searching/filtering/where-object

Viewing 6 reply threads
  • Author
    Posts
    • #26761
      Participant
      Topics: 10
      Replies: 48
      Points: 0
      Rank: Member

      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
      Participant
      Topics: 10
      Replies: 48
      Points: 0
      Rank: Member

      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
      Participant
      Topics: 16
      Replies: 108
      Points: 0
      Rank: Member

      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
      Participant
      Topics: 10
      Replies: 48
      Points: 0
      Rank: Member

      awesome.. thanks for the suggestion.

    • #26806
      Participant
      Topics: 2
      Replies: 376
      Points: 1
      Rank: Member

      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
      Participant
      Topics: 12
      Replies: 1623
      Points: 2,565
      Helping Hand
      Rank: Community Hero

      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
      Participant
      Topics: 10
      Replies: 48
      Points: 0
      Rank: Member

      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

Viewing 6 reply threads
  • The topic ‘searching/filtering/where-object’ is closed to new replies.