In this situation, you should use double backticks with single quotes to avoid parentheses. You can also use quadruple backticks when using strings in double quotes.
Thus, a fixed line of code:
Copy-item -Path $FSG\$SW\0.RoomView.Notes\starter\'''[RoomView''] Versions explained*.pdf' -Destination $FSG\$containerFolder\$rootFolder\'Fusion Server'\
Another good source of information about file paths, wired characters, etc. - read this article: Taking things (like file paths) literally
EDIT
Thanks @ mklement0 for emphasizing that the true reason for this mismatch is due to an error currently in PowerShell 1 . This error escapes wildcards, as well as quotes with the -Path option, behave differently by default than other options, such as the -Include and -Filter .
To expand on @ mklement0 excellent answer , and comments , and other answers below:
To better understand why we need single quotes and double-ticks in this situation ; ( and to highlight the bug bug and inconsistencies), let's look at a few examples to demonstrate what happens:
Get-Item and the associated cmdlets ( Get-ChildItem , Copy-Item , etc.) handle the -Path parameter differently when dealing with a combination of escaped characters , escaped characters, and escaped wildcards * at the same time *** !
TLDR The main reason we need a combination of single quotes and double backticks is because the underlying PowerShell provider parses the -Path parameter string for wildcards. It looks like it is parsed once for escape characters and a second time for wildcard evaluation.
Let's look at a few examples to demonstrate this strange result:
First, let's create two files for testing with the names File[1]a.txt and File[1]b.txt
"MyFile" | Set-Content '.\File'[1']a.txt' "MyFriend" | Set-Content '.\File'[1']b.txt'
We will try different ways to get the file. We know that the square brackets [ ] are wildcards wildcards , so we need to escape them with the backtick character .
We will try to get one file explicitly.
Let's start by using single quotes for letter strings:
PS C:\> Get-Item 'File[1]a.txt' PS C:\> Get-Item 'File'[1']a.txt' Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2019-09-06 5:42 PM 8 File[1]a.txt PS C:\> Get-Item 'File''[1'']a.txt' Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2019-09-06 5:42 PM 8 File[1]a.txt
For strings in single quotes, all that is required to extract the file is a single backquote, but two backquotes also work.
Using strings in double quotes, we get:
PS C:\> Get-Item "File[1]a.txt" PS C:\> Get-Item "File'[1']a.txt" PS C:\> Get-Item "File''[1'']a.txt" Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2019-09-06 5:42 PM 8 File[1]a.txt
For double-quoted strings, as expected, we see that we need two backslashes for this to work.
Now we want to get both files , and , use a wildcard.
Let's start with single quotes:
PS C:\> Get-Item 'File[1]*.txt' PS C:\> Get-Item 'File'[1']*.txt' PS C:\> Get-Item 'File''[1'']*.txt' Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2019-09-06 5:42 PM 8 File[1]a.txt -a---- 2019-09-06 5:49 PM 10 File[1]b.txt
In single quotes, when we have a wildcard character, we need two sets of backticks. One to avoid the brackets, and a second backslash to avoid the backslash, which we used to avoid the brackets when the wildcard is calculated.
Similarly for double quotes:
PS C:\> Get-Item "File[1]*.txt" PS C:\> Get-Item "File'[1']*.txt" PS C:\> Get-Item "File''[1'']*.txt" PS C:\> Get-Item "File'''[1''']*.txt" PS C:\> Get-Item "File''''[1'''']*.txt" Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2019-09-06 5:42 PM 8 File[1]a.txt -a---- 2019-09-06 5:49 PM 10 File[1]b.txt
With double quotes, evaluate in more detail with a wildcard. In this case, we need four sets of back ticks. For double quotes, we need two back quotes to go beyond the brackets, and two more back quotes to escape the escape characters when it comes to evaluating a star wildcard.
EDIT
As @ mklement0 mentions , this behavior with the -Path parameter is inconsistent and behaves differently than the -Include parameter, where only one -Include to correctly exit the brackets. This may be โfixedโ in a later version of PowerShell.
1 As of Windows PowerShell v5.1 / PowerShell Core 6.2.0-preview.3