Bug in PowerShell classes when script is in a folder containing a single-quote?

1

My client's PowerShell scripts aren't working for a new user who happens to have a single-quote in their name - which means their profile path is called "C:\Users\Liam'OReilly". It appears to be a bug in the PowerShell class system.

Create a folder called "C:\Temp'Test" and add Test.ps1:

Write-Host "Test"
Class MyClass {}

Execute this script and you get this error:

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) At line:1 char:1 + & 'C:\Temp''Test\Test..ps1' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], FileLoadException + FullyQualifiedErrorId : System.IO.FileLoadException

Rename the folder as simply C:\Test and it runs without an error.

Anyone got a workaround and what would be the best way to report this bug?

My workaround right now (as the user starts on Monday) is to remove the single-quote character from their display name in Office 365 which isn't ideal.

windows-10
powershell
powershell-5.0
asked on Super User May 17, 2018 by munrobasher

1 Answer

0

The StackOverflow post How to access file paths in Powershell containing special characters? has three solutions to the problem :

  1. Use the new escape character % to stop the normal parsing of the command line up to the end of the line to not match quotes, not stop at semicolon and not expand PowerShell variables. Environment variables are still expanded when using cmd.exe syntax (e.g. %TEMP%).

  2. Write the file name to a temporary file and use it from there.

  3. Use the backtick (`) symbol to escape the special character. For example :

    $dir = "C:\folder`$name\dir"
    
answered on Super User May 17, 2018 by harrymc

User contributions licensed under CC BY-SA 3.0