Home Dashboard Directory Help
Search

"Run with PowerShell" feature doesn't work on Windows 7 by Aleksandar Nikolić


Status: 

Active


5
0
Sign in
to vote
Type: Bug
ID: 775161
Opened: 12/26/2012 4:11:35 PM
Access Restriction: Public
2
Workaround(s)
view
2
User(s) can reproduce this bug

Description

"Run with PowerShell" feature runs as expected on Windows 8 computer. If we compare Registry value that defines a command that runs when we select "Run with PowerShell" on a contextual menu, we can see that commands are quite different. Why it fails on Windows 7 (and probably Server 2008/2008 R2; I haven't tested that)? -File and -Command parameters cannot be used in the same command.

On Windows 8:
C:\PS> Get-ItemProperty REGISTRY::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command | Select -Expand "(default)"

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -Command "if((Get-ExecutionPolicy ) -ne 'AllSigned'){ Set-ExecutionPolicy -Scope Process Bypass }; & '%1' %*

On Windows 7:
PS C:\> Get-ItemProperty REGISTRY::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command | Select -Expand "(default)"

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-file" "%1" "-Command" "if((Get-ExecutionPolicy ) -ne AllSigned) { Set-ExecutionPolicy -Scope Process Bypass }"
Details
Sign in to post a comment.
Sign in to post a workaround.
Posted by Randy in Marin on 2/11/2014 at 6:12 PM
# script to test and change the registry setting - use at your own risk

cls

$w7psh3shellcmd = @'
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-file" "%1" "-Command" "if((Get-ExecutionPolicy ) -ne AllSigned) { Set-ExecutionPolicy -Scope Process Bypass }"
'@

$w8psh3shellcmd = @'
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -Command "if((Get-ExecutionPolicy ) -ne 'AllSigned'){ Set-ExecutionPolicy -Scope Process Bypass }; & '%1' %*
'@

$shellcmd = Get-ItemProperty -path REGISTRY::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command | Select -Expand "(default)"

if ($shellcmd -eq $w7psh3shellcmd) {

    $caption = "Update Registry"
    $message = 'The current version of the "Run with PowerShell" has a known issue. Replace?'
    $yes = new-Object System.Management.Automation.Host.ChoiceDescription "&Yes","help"
    $no = new-Object System.Management.Automation.Host.ChoiceDescription "&No","help"
    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
    $answer = $host.ui.PromptForChoice($caption,$message,$choices,0)

    if ($answer -eq 0) {
        Set-ItemProperty -path REGISTRY::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command -name '(Default)' -value $w8psh3shellcmd
        write-host 'Updated.'
    } else {
        write-host 'Updated was skipped.'
    }
    
} else {
    write-host 'Updated is not needed.'
}
Posted by Rohn Edwards on 2/7/2013 at 8:58 AM
If you can't change the command that the context menu runs to match what Windows 8 uses, and you still want to be able to use the context menu, you can create an optional parameter called 'Command' in the script file. This will catch the -Command argument being passed to powershell.exe (it should contain the string 'if((Get-ExecutionPolicy ) -ne AllSigned) { Set-ExecutionPolicy -Scope Process Bypass }').