Square Brackets in File Names

Welcome Forums General PowerShell Q&A Square Brackets in File Names

  • This topic has 2 replies, 3 voices, and was last updated 9 months ago by
Viewing 2 reply threads
  • Author
    • #222660
      Topics: 1
      Replies: 0
      Points: 17
      Rank: Member


      I am a retired software developer just working on my music library.
      My problem is with square brackets in file names which are valid names but cause problems in my scripts.
      I wrote a simple script to illustrate. The directories in the working directory should be listed
      with a count of the files in them. Each directory has 1 file. Filenames with matching square brackets
      show zero files. Filenames with unmatched square brackets cause an error, indicating an invalid
      wildcard pattern. I am not a “regular expression” guru but the filename seems to be being evaluated as such?
      Here is the script and output”

      PS C:\Users\jhibn_000\powershell\Recs\xx’ yy>
      $parent = get-item .
      $parentDirs = $parent.GetDirectories()
      for($i=0; $i -lt $parentDirs.Count; $i++) {

      $children = get-childitem $parentDirs[$i].FullName -file
      $parentDirs[$i].FullName + ” has ” + $children.Count
      get-host | Select-Object -property version

      Directory: C:\Users\jhibn_000\powershell\Recs\xx’ yy

      Mode LastWriteTime Length Name
      —- ————- —— —-
      d—– 4/21/2020 12:38 AM All Aboard!
      d—– 9/18/2014 1:20 AM AnotherSide+Times{2_2}
      d—– 9/18/2014 1:20 AM AnythingGoes+[]SweetCharity1
      d—– 9/18/2014 1:20 AM BackRoads+[NashvilleSkyline
      d—– 4/26/2020 6:40 PM xx=.-cc
      d—– 4/26/2020 6:40 PM zz=.-cc
      d—– 9/18/2014 1:20 AM _+PlanetWaves
      C:\Users\jhibn_000\powershell\Recs\xx’ yy\All Aboard! has 1
      C:\Users\jhibn_000\powershell\Recs\xx’ yy\AnotherSide+Times{2_2} has 1
      C:\Users\jhibn_000\powershell\Recs\xx’ yy\AnythingGoes+[]SweetCharity1 has 0
      Get-ChildItem : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not
      valid: BackRoads+[NashvilleSkyline
      At line:7 char:15
      + $children = get-childitem $parentDirs[$i].FullName -file
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
      + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetChildItemCommand

      C:\Users\jhibn_000\powershell\Recs\xx’ yy\BackRoads+[NashvilleSkyline has 0
      C:\Users\jhibn_000\powershell\Recs\xx’ yy\xx=.-cc has 1
      C:\Users\jhibn_000\powershell\Recs\xx’ yy\zz=.-cc has 1
      C:\Users\jhibn_000\powershell\Recs\xx’ yy\_+PlanetWaves has 1

      Version : 5.1.18362.752

      I’m sure I can write something to change the offending names but would rather not. Is it possible to avoid this?


    • #222666
      Topics: 10
      Replies: 2480
      Points: 6,537
      Helping Hand
      Rank: Community MVP

      Joe, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code or error messages or sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      Have you tried to search for it. This question raises about once a month and has been answered several times even here in this forum. 😉

    • #222723
      Topics: 32
      Replies: 852
      Points: 2,780
      Helping Hand
      Rank: Community Hero

      You’d have to use the -literalpath option if the name has square brackets. They’re considered wildcards, like “dir [a-z]*”.

Viewing 2 reply threads
  • The topic ‘Square Brackets in File Names’ is closed to new replies.