Reputation: 33
I have been at great pains to see where I've gone wrong here in adding double quotes to file name text. The following code works fine under the -whatif umbrella but not without.
$a = "20`" board"
get-childitem | rename-item -newname {$_.fullname -replace "20 board", $a } -whatif
get-childitem | rename-item -newname {$_.fullname -replace "20 board","20`" board" } -whatif
The desired outcome is to replace 20 board
with 20" board
.
The above snippets give me a rename-item : Illegal characters in path.
error.
Upvotes: 3
Views: 5401
Reputation: 437698
ArcSet's helpful answer contains the crucial pointer: "
characters cannot be used in file and folder names on Windows.
There is a - suboptimal - approximation of the desired functionality: you can use a non-ASCII double-quotation mark, which is visually similar - but note that you'll need to use that exact quotation mark to refer to that file later (unless you use wildcards to match it), and the very visual similarity can therefore lead to confusion.
'hi' > "20`“ board" # works, because “ is not a regular double quote
“
is Unicode character U+201C
, the LEFT DOUBLE QUOTATION MARK.
Note that PowerShell treats regular double quotes and “
as interchangeable, which is why you still need to `
-escape “
inside a regular double-quoted string.
Upvotes: 3
Reputation: 6860
In Windows you cannot use certain chars in a file or folder name; a double quote is one of those.
Read more at https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file.
Excerpt from that page:
The following reserved characters [are not allowed]:
< (less than)
(greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
Upvotes: 4