Exclude folders with special characters when using Get-Childitem

Welcome Forums General PowerShell Q&A Exclude folders with special characters when using Get-Childitem

Viewing 8 reply threads
  • Author
    Posts
    • #236536
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      I have folders with %, _ and () in the names, how can I exclude these chars?
      Get-ChildItem c:\ -Name -Directory

    • #236557
      Participant
      Topics: 3
      Replies: 421
      Points: 1,479
      Helping Hand
      Rank: Community Hero

      What do you mean by exclude them? Do you mean ignore?

    • #236569
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      Yes, I would like to ignore them.

    • #236572
      Participant
      Topics: 3
      Replies: 421
      Points: 1,479
      Helping Hand
      Rank: Community Hero

      My questions aren’t the best, I’m still lacking context. What is the issue? Are you getting errors? Are you wanting to list all except those with special characters?

    • #236578
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      My questions aren’t the best, I’m still lacking context. What is the issue? Are you getting errors? Are you wanting to list all except those with special characters? – Yes

      I finally managed to get the command: Get-ChildItem C:\ | Where-Object Name -notmatch “^%” but now I’m battling to add the other characters

    • #236587
      Participant
      Topics: 1
      Replies: 4
      Points: 25
      Rank: Member

      This will return objects that contain numbers and letters only. i.e. no special characters 🙂

      Get-ChildItem c:\ | Where-Object Name -match "^[a-zA-Z0-9]+$"

       

    • #236602
      Participant
      Topics: 5
      Replies: 323
      Points: 473
      Helping Hand
      Rank: Contributor

      To exclude just those characters, use the line below.  Separate multiple characters with ‘|’

      Get-ChildItem -Directory | Where-Object {$_.Name -notmatch '%|_|\(|\)'}
    • #236704
      Participant
      Topics: 3
      Replies: 421
      Points: 1,479
      Helping Hand
      Rank: Community Hero

      I also initially thought “wouldn’t it be easier to include than exclude?” but then I recalled just how many special characters there are. This regex pattern matches the characters you listed.

      '(?:([%,_()]*))',''
      

      Here is a test you can run to confirm it will match all.

      @'
      test1%
      test2,
      test3_
      test4(
      test5)
      test6%,_()
      %t,e_s(t)7
      '@ -replace '(?:([%,_()]*))',''
      

      It can be simplified to this so long as you leave it as an object with a name property. You can let it be extracted as part of the where-object process instead of -name in get-childitem or deal with it later on.

      Get-ChildItem c:\ -Directory | where name -NotMatch '[%|,|_|(|)]'
      

      Then you still have your object and if you need to get down to just the name you have the many normal powershell ways.

      Get-ChildItem c:\ -Directory | where name -NotMatch '[%|,|_|(|)]' | select -exp name
      
      (Get-ChildItem c:\-Directory | where name -NotMatch '[%|,|_|(|)]').name
      
      $dirs = Get-ChildItem c:\ -Directory | where name -NotMatch '[%|,|_|(|)]'
      $dirs.name
      

      Take care.

    • #236722
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      This will return objects that contain numbers and letters only. i.e. no special characters 🙂

      <textarea class=”ace_text-input” style=”opacity: 0; height: 17.6px; width: 6.59775px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      Get-ChildItem c:\ | Where-Object Name -match “^[a-zA-Z0-9]+$”
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      This returns exactly what i’m looking for. thanks for the assist

Viewing 8 reply threads
  • You must be logged in to reply to this topic.