2

I noticed that Windows 10 (at least the current stable build 1607) does not properly escape filenames when you try to open a PowerShell script (.ps1) via context menu » Edit.

While ampersand and spaces are treated correctly, Windows cannot open a file whose name has commas - the file name is truncated at the comma.

enter image description here

Resulting error

enter image description here

I am looking for some solution to this problem, whether via a registry fix or a way to add quotes, to escape the comma or anything else.

1
  • I get the same thing. Using open with > notepad seems to work, though. But yeah, Windows is definitely not handling commas correctly.
    – Narzard
    Commented Aug 19, 2016 at 19:12

2 Answers 2

5
+50

You need to change value in that registry key HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Edit\Command from "C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe" "%1" to "C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe" """%1""". Or you can use following PowerShell command to do that:

Set-ItemProperty HKLM:\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Edit\Command '(default)' '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe" """%1"""'
0
1

Building on @user364455's answer, this modifies both the >Edit and >Open context menu items, allowing you to open .ps1 files (with commas in the path) in ISE with elevation by either double-clicking on a .ps1 file or right-clicking in Explorer and choosing 'Edit'. A here-string was used to save the headache of escaping quotes. A PowerShell console window unfortunately pops up for a split second, which there are workarounds for (the most common one I've found involves VBScript).

PowerShell:

$newValue = @'
PowerShell.exe -NoProfile -WindowStyle Hidden -NonInteractive -Command "& {Start-Process -FilePath C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe -ArgumentList '""""""""%1""""""""' -Verb RunAs}"
'@

# Open
Set-ItemProperty -Path HKLM:\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Open\Command -Name '(default)' -Value $newValue -Force

# Edit
Set-ItemProperty -Path HKLM:\SOFTWARE\Classes\SystemFileAssociations\.ps1\Shell\Edit\Command -Name '(default)' -Value $newValue -Force

.reg version

Windows Registry Editor Version 5.00

; Open
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]
@="PowerShell.exe -NoProfile -WindowStyle Hidden -NonInteractive -Command \"& {Start-Process -FilePath C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe -ArgumentList '\"\"\"\"\"\"\"\"%1\"\"\"\"\"\"\"\"' -Verb RunAs}\""

; Edit
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.ps1\Shell\Edit\Command]
@="PowerShell.exe -NoProfile -WindowStyle Hidden -NonInteractive -Command \"& {Start-Process -FilePath C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe -ArgumentList '\"\"\"\"\"\"\"\"%1\"\"\"\"\"\"\"\"' -Verb RunAs}\""

You must log in to answer this question.

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