# Rename Files error which has contain \[\] in name

**URL:** <https://forums.powershell.org/t/rename-files-error-which-has-contain-in-name/22694>\
**Category:** PowerShell Help\
**Created:** [August 2, 2023, 12:27am UTC](https://forums.powershell.org/t/rename-files-error-which-has-contain-in-name/22694 "2023-08-02T00:27:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pozso123](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@pozso123](https://forums.powershell.org/u/pozso123)\
**Post date:** [August 2, 2023, 12:27am UTC](https://forums.powershell.org/t/rename-files-error-which-has-contain-in-name/22694/1 "2023-08-02T00:27:09Z")

</div>

I have this script to modify some filename:

```auto
$folderPath = "E:\Új mappa"

Get-ChildItem -Path $folderPath -File | ForEach-Object {
    $oldName = $_.Name
    $newName = $oldName.Replace("[", "").Replace("]", "").Replace("(", "").Replace(")", "").Replace("{", "").Replace("}", "").Replace("<", "").Replace(">", "").Replace("\\", "")
    $newPath = Join-Path -Path $folderPath -ChildPath $newName
    Rename-Item -Path $_.FullName -NewName $newName -WhatIf
}

```

The 3 files to test contain `[]`  
. So it only report error which has containinng  
` []`

in their name :  
file stucture:  
1 [dsadasdasd.txt  
2 asd.ps1  
3 dsadada[sdf]afsfasdasd.txt

error:

```auto
What if: Performing the operation "Rename File" on target "Item: E:\Új mappa\asd.ps1 Destination: E:\Új mappa\asd.ps1".
Rename-Item : Cannot rename because item at 'E:\Új mappa\dsadada[sdf]afsfasdasd.txt' does not exist.
At line:6 char:5
+ Rename-Item -Path $_.FullName -NewName $newName -WhatIf
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

Rename-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: [dsa
dasdasd.txt
At line:6 char:5
+ Rename-Item -Path $_.FullName -NewName $newName -WhatIf
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Rename-Item], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.RenameItemCommand

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 2, 2023, 12:39am UTC](https://forums.powershell.org/t/rename-files-error-which-has-contain-in-name/22694/2 "2023-08-02T00:39:32Z")

</div>

Since the characters you want to replace are actually meta characters you have to escape them. Or you use a character class like this:

```auto
$oldName = 'Wel[come]Back(To)The{PowerShell}.<org>Fo/rum'
$oldName -replace '[][(){}<>\\/]'

```

This way you only have to explicitly escape the backslash. 😉

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [August 2, 2023, 2:06am UTC](https://forums.powershell.org/t/rename-files-error-which-has-contain-in-name/22694/3 "2023-08-02T02:06:38Z")

</div>

> [@pozso123](#):
>
> ```auto
> Get-ChildItem -Path $folderPath -File | ForEach-Object {
> $oldName = $_.Name
> $newName = $oldName.Replace("[", "").Replace("]", "").Replace("(", "").Replace(")", "").Replace("{", "").Replace("}", "").Replace("<", "").Replace(">", "").Replace("\\", "")
> $newPath = Join-Path -Path $folderPath -ChildPath $newName
> Rename-Item -Path $_.FullName -NewName $newName -WhatIf
> }
> 
> ```

-Path parameter accepts wildcards, `]` and `[` are wildcard characters. Therefore, you should use -LiteralPath as it takes that, a literal path (no wildcards)

```auto
Get-ChildItem -Path $folderPath -File | ForEach-Object {
    $oldName = $_.Name
    $newName = $oldName.Replace("[", "").Replace("]", "").Replace("(", "").Replace(")", "").Replace("{", "").Replace("}", "").Replace("<", "").Replace(">", "").Replace("\\", "")
    $newPath = Join-Path -Path $folderPath -ChildPath $newName
    Rename-Item -LiteralPath $_.FullName -NewName $newName -WhatIf
}

```

---

<div class="post-metadata">

**Author:** ![pozso123](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@pozso123](https://forums.powershell.org/u/pozso123)\
**Post date:** [August 2, 2023, 8:30pm UTC](https://forums.powershell.org/t/rename-files-error-which-has-contain-in-name/22694/4 "2023-08-02T20:30:26Z")

</div>

It works only the literalpath, thank you for solution

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/rename-files-error-which-has-contain-in-name/22694/5 "2024-05-16T20:14:45Z")

</div>


