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.

munrobasher

Posted 2018-05-17T10:23:36.587

Reputation: 640

Honestly the best would be to remove the single quote from the name on the domain side. You can still keep the single quote in the display name, so the only change, would be the directory that is created on a domain connected computer. Otherwise you have to manually modify the registry on each device connected to the domain they log into. As for reporting the bug, report it to the PowerShell Core team, should be fixed in PowerShell 5.

– Ramhound – 2018-05-17T10:44:51.187

Scratch that. PowerShell 5.1 appears to be the last (closed source) version. By reporting it to the PowerShell Core team it will be fixed eventually. You should verify the current beta version of PowerShell Core does not have this problem though (before you report it) – Ramhound – 2018-05-17T10:51:19.873

Thx - in Office 365/Azure AD I had to rename the display name to "Liam OReilly" as this appears to be what controls the name of the user's profile directory – munrobasher – 2018-05-17T10:51:43.300

Problem was already reported and fixed to the PowerShell Core team by the way. I went ahead and flagged this as a duplicate, since the issue was reported, and it referenced a question asked here. My assumption is that by the reported issue being fixed, while slightly different, the problem you discovered will also be fixed. – Ramhound – 2018-05-17T10:55:32.843

Whilst this flaw is definitely in the same area, it's not the same bug as referred above. You can run the script using the File Explorer content menu (so that bug has been fixed) but this is a different error with the class statement – munrobasher – 2018-05-17T11:01:43.687

Then I encourage you to report the issue, but based on what read, the behavior you discovered is more or less intended based on design choices. Now I won't argue a way around that intended design choice should exist but it seems the problem surrounding a single quote existing in the directory is because of a long standing design decision within PowerShell itself (and it likely will not be fixed in PowerShell 5.1 so you will have to use PowerShell Core if it is fixed) – Ramhound – 2018-05-17T11:04:09.273

Answers

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"
    

harrymc

Posted 2018-05-17T10:23:36.587

Reputation: 306 093

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 – 2018-05-17T11:34:46.013

@Ramhound: This isn't one of the suggestions. Would you mind expanding why two of them are unusable? – harrymc – 2018-05-17T12:20:29.827

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 – 2018-05-17T13:34:40.653

@Ramhound: The poster can use a PowerShell script to run the single-quote script using one of the above methods. – harrymc – 2018-05-17T14:04:38.123

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 – munrobasher – 2018-05-27T09:43:28.273

A PowerShell script can be run from any folder. Your problem is passing a folder name in a format that isn't understood as something else. My suggestion is two-step : call a script that will convert its argument as above and pass it to the main script in that format. – harrymc – 2018-05-27T09:55:56.603