PowerShell for Windows Admins

Apr 14 2019   10:41AM GMT

Brackets in file names

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


If you have brackets – [ or ] – in your file name you can see problems working with the file name. This is how to deal with brackets in file names.

Consider the files:


which have a selection of brackets in the file name.

If you try something like

Get-ChildItem -Path c:\test | foreach {Rename-Item -Path $_.Fullname -NewName “New$($_.name)”}

You’ll find that two files

are successfully renamed but the other two fail with an error like

Rename-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: out[data02.txt

Notice its the two files with left hand brackets that fail.

Its because PowerShell its treating the bracket as the opening of a wildcard / regex pattern.

The easiest way round this problem is to use –Literalpath instead of –Path

Get-ChildItem -Path c:\test | foreach {Rename-Item -LiteralPath $_.Fullname -NewName “New$($_.name)”}

And you’ll find all of the files rename successfully.

LiteralPath treats what ever its given literally – it doesn’t do the interpretation that Path attempts.

If you think you’re going to have odd characters in your file names then remember to use literalpath – you’ll find it as an alternative on all the core cmdlets that accept paths

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: