powershellfilenamesillegal-characters

writing double quotes to file name text with powershell


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.


Solution

  • 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)