1

This code:

$username = 'Username'
$password = 'Password'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process -FilePath powershell -WorkingDirectory "$env:ALLUSERSPROFILE" -Credential $credential -WindowStyle Hidden -ArgumentList "-NoProfile -Command `"Start-Process -FilePath wscript -Verb RunAs -ArgumentList '\`"$((Get-Location).Path -replace "'", "''")\test.vbs\`" \`"/CurrentDirectory:$((Get-Location).Path -replace "'", "''")\`" \`"/AppData:$($env:APPDATA -replace "'", "''")\`"'`""

works when I run it line by line from the PowerShell command prompt.
The problem occurs both when I associate the .ps1 extension to "Windows PowerShell" and then double-click on the script, and when I compile the script in .exe through IronMan Software's "PSScriptPad".
A similar problem is present at the following link:
Powershell script executes correctly when I choose "run with powershell", but not when I "open with" powershell or double-click it
but in my case it doesn't even work with "Run with PowerShell" and perhaps the cause of the problem is the same one that also affects executables.

Another useful link is the following:
https://stackoverflow.com/a/58245206/45375

How can I solve the problem?
Windows 10 Pro 64-bit
Powershell Version: 5.1.19041.1237 (Integrated in Windows 10).

0

1 Answer 1

2

The compiled executable works correctly when it is not in a directory containing the apostrophe characters while in the case of double-clicking on a .ps1 script, the directory containing it cannot even contain spaces.
This is a useful link:
https://social.technet.microsoft.com/Forums/en-US/bf3344de-3af6-48e3-9f43-f595bb41c62d/bug-powershell-starts-w-error-when-opened-by-context-menu-in-folder-w-apostrophe-in-its-name?forum=win10itprogeneral

For the apostrophes path problem in .exe files, I sent a bug report to IronMan Software PSScriptPad, which was then taken care of.
For the problem of paths with apostrophes or consecutive whitespaces when double-clicking or doing "Run with PowerShell" on .ps1 scripts, I have solved by fixing the following registry values:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.ps1]
@="Microsoft.PowerShellScript.1"

[HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell\command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -Command \"Set-Location -LiteralPath \\\"%V\\.\\\"\""

[HKEY_CLASSES_ROOT\Directory\Shell\Powershell\command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -Command \"Set-Location -LiteralPath \\\"%V\\.\\\"\""

[HKEY_CLASSES_ROOT\Drive\shell\Powershell\command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -Command \"Set-Location -LiteralPath \\\"%V\\.\\\"\""

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\DefaultIcon]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\",0"

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -Command \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force }; & \\\"%1\\\"\""

[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\0\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -Command \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force }; & \\\"%1\\\"\""

[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\Edit\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe\" -File \"%1\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell]
"ExecutionPolicy"="RemoteSigned"
Sign up to request clarification or add additional context in comments.

12 Comments

As for double-clicking on a .ps1 script: This answer contains a run-once programmatic solution that from then on runs PowerShell script files in a stay-open PowerShell window, and paths with spaces as well as apostrophes (single-quotes, ') are supported too.
Nice - of course, this is much more comprehensive than just defining a double-click. There's a hitch, however: HKEY_CLASSES_ROOT\Drive\shell\Powershell\command, presumably among others, cannot be written to, at least on my Windows 10 20H2, machine due to only the TrustedInstaller having write permissions. Aside from that, worth pointing out that you need elevation (run as admin) to write this to the registry. And (which I assume isn't a problem for you) that it is a Windows PowerShell-only solution. To be extra nice to future readers you could show how to write the file programmatically.
A quibble (it's not a problem as written, because nothing follows the Set-Location call): Since a path with a trailing \ (e.g. C:\ ) is passed to the HKEY_CLASSES_ROOT\Drive\shell\Powershell\command command, you technically need to escape that trailing \ . From cmd.exe, compare powershell.exe -Command "write-output -LiteralPath \"C:\\"; get-date" (broken) to powershell.exe -Command "write-output -LiteralPath \"C:\\\"; get-date" (OK).
Also in HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell\command, because "background" means when you right click on the "empty" part of a window of Windows Explorer, and you can easily open `C:\` folder with GUI and right-click in a empty zone. Thank you.
I have solved this quibble with this: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit -Command "Set-Location -LiteralPath \"%V/\"; get-date" because are accepted also paths with slashes instead of backslashes. Also mix slashes and backslashes. Then "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit -Command "Set-Location -LiteralPath \"C:\/\"; get-date" Also multiple consecutive slashes and/or consecutive backslashes are accepted in the paths. Genial
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.