brackets cause PowerShell scripts to fail
- Sean O'Shea
- May 29
- 2 min read
Keep in mind when you're drafting a PowerShell script that if any of the filepaths referenced in it contain beginning or ending brackets, the script will fail. So if we set up a simple copy-item script like this in Excel:

. . . which copies files from one location to a new one, renamed with a new filename, but the filepaths include a bracketed word, the script will fail without giving an error message that something is wrong.

The files in the first location are simply not copied to the new folder at all, and no error message flags the problem in the PowerShell blue console at the bottom.
We can get around this problem by adding a backtick ` in front of each beginning and ending bracket in the script. (Be sure to use a backtick and not an apostrophe.) You can enter a backtick by pressing ALT + 096. If your filepath is listed in double quotes, use two backticks in front of each bracket; if the path is enclosed in single quotes use just one backtick.

So the updated script will look like this:
copy-item -Path "C:\foofolder\test\powershell\Exhibit 001.pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit A.pdf"
copy-item -Path "C:\foofolder\test\powershell\Exhibit 002 ``[redacted``].pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit B.pdf"
copy-item -Path "C:\foofolder\test\powershell\Exhibit 003.pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit C.pdf"
copy-item -Path "C:\foofolder\test\powershell\Exhibit 004 ``[redacted``].pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit D.pdf"
copy-item -Path "C:\foofolder\test\powershell\Exhibit 005.pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit E.pdf"
copy-item -Path "C:\foofolder\test\powershell\Exhibit 006 ``[redacted``].pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit F.pdf"
copy-item -Path "C:\foofolder\test\powershell\Exhibit 007.pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit G.pdf"
copy-item -Path "C:\foofolder\test\powershell\Exhibit 008 ``[redacted``].pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit H.pdf"
copy-item -Path "C:\foofolder\test\powershell\Exhibit 009.pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit I.pdf"
copy-item -Path "C:\foofolder\test\powershell\Exhibit 010 ``[redacted``].pdf" -Destination "C:\foofolder\test\powershell\Exhs A to Z\Exhibit J.pdf"
. . . and give you the result you wanted:
