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: 6
      Replies: 552
      Points: 2,090
      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: 6
      Replies: 552
      Points: 2,090
      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 🙂

       

    • #236602
      Participant
      Topics: 5
      Replies: 328
      Points: 520
      Helping Hand
      Rank: Major Contributor

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

    • #236704
      Participant
      Topics: 6
      Replies: 552
      Points: 2,090
      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.

      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.

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

      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
  • The topic ‘Exclude folders with special characters when using Get-Childitem’ is closed to new replies.