title: Renaming a file that has a bracket in the file name using power shell
body: <p>I am trying to rename a file that has a bracket in the file name. This does not seem to work because powershell sees [] as special characters and does not know what to do.</p>
  
  <p>I have a folder on my computer c:\test. I want to be able to look through that folder and rename all files or portions of the file. The following code seems to work but if the file has any special characters in it the code fails:</p>
  
  <pre><code>Function RenameFiles($FilesToRename,$OldName,$NewName){
  
      $FileListArray = @()
      Foreach($file in Get-ChildItem $FilesToRename -Force -Recurse  | Where-Object {$_.attributes -notlike "Directory"})
      {
          $FileListArray += ,@($file)
      }
  
      Foreach($File in $FileListArray)
      {
          IF ($File -match $OldName )
          {
              $File | rename-item -newName {$_ -replace "$OldName", "$NewName" }
          }
      }
  }
  
  renamefiles -FilesToRename "c:\test" -OldName "testt2bt" -NewName "test"
  </code></pre>
  
  <p>I did find a similar question: <a href="https://stackoverflow.com/questions/4089028/replace-square-bracket-using-powershell">Replace square bracket using Powershell</a>, but I can't understand how to use the answer cause it's just a link explaining the bug:</p>