3

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.

9

1 Answer 1

1

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"
    
6
  • Sadly, only one of your solutions can be used by the author, running the script from another directory. Without knowing anything about the script that might not be possible.
    – Ramhound
    May 17, 2018 at 11:34
  • @Ramhound: This isn't one of the suggestions. Would you mind expanding why two of them are unusable?
    – harrymc
    May 17, 2018 at 12:20
  • If the script was contained in a folder with a single quote, the script does not even run, so using a variable or using an escape character within the script, won't solve the author's problem.
    – Ramhound
    May 17, 2018 at 13:34
  • @Ramhound: The poster can use a PowerShell script to run the single-quote script using one of the above methods.
    – harrymc
    May 17, 2018 at 14:04
  • Only running from a different folder is a possible solution and that's difficult as the script is been run from a user profile folder which has the quote in it. Might be able to copy the script to another folder if it finds a single quote but very messy May 27, 2018 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .