0

This is a frustrating error I keep getting:

Every other application I get I can right click it and get this (Blacked out some personal details.):

Screenshot.

Run as administrator is clearly there and works fine, but when it comes to .py files I get this:

Screenshot.

For some reason there's just no run as administrator.

0

2 Answers 2

1

You can modify the registry to add Run as Administrator option for Python files to context menu.

Here is how to do it:

Step 1: Open elevated PowerShell:

Win+R→type powershellCtrl+Shift+Enter

Step 2: Paste these commands:

$python=where.exe Python.exe
cmd /c "assoc .py=Python.File"
cmd /c $('ftype Python.File="{0}" "%1" "%*"' -f $python)
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
Set-ItemProperty -Path "HKCR:\.py" -Name "(Default)" -Type String -Value "Python.File"
Set-ItemProperty -Path "HKCR:\.py" -Name "Content Type" -Type String -Value "text/plain"
New-Item -Path "HKCR:\Python.File\shell" -ErrorAction SilentlyContinue | Out-Null
New-Item -Path "HKCR:\Python.File\shell\runas" -ErrorAction SilentlyContinue | Out-Null
Set-ItemProperty -Path "HKCR:\Python.File\shell\runas" -Name "(Default)" -Type String -Value "Run as Administrator"
Set-ItemProperty -Path "HKCR:\Python.File\shell\runas" -Name "HasLUAShield" -Type String -Value '""'
New-Item -Path "HKCR:\Python.File\shell\runas\command" -ErrorAction SilentlyContinue | Out-Null
$Command='cmd.exe /S /K "{0}" \"%1\" %*' -f $python.replace("\","\\")
Set-ItemProperty -Path "HKCR:\Python.File\shell\runas\command" -Name "(Default)" -Type String -Value $Command
Set-ItemProperty -Path "HKCR:\Python.File\shell\runas\command" -Name "DelegateExecute" -Type String -Value '""'

Then the job should be done, enjoy.


The first line uses where.exe to find where the Python.exe is, if Python.exe is in PATH environment variable, problem is if there are multiple Python.exe the result of it isn't a one line string, there would be multiple lines, try to run:

where.exe Python.exe 

To check if there are multiple lines, if there is only one line use:

$Python=where.exe Python.exe

If there are multiple lines, use:

$Python=where.exe Python.exe | Select-Object -First 1

Or manually supply the path to desired Python, for example:

$Python="C:\Program Files\Python39\Python.exe"

Note you can get its path by using where.exe, and in PowerShell even if the string doesn't contain spaces you have to use quotes.

The second line and third line fix openwith associations for .py files, problem is assoc and ftype are cmd commands and there is no executable in C:\Windows\System32 for them, so the safest way is to use cmd /c to run them, however I use PowerShell 7.1.1 and I vaguely remember having succeeded in using one of them without cmd /c part, please run:

Test-Path C:\Windows\System32\assoc.exe
Test-Path C:\Windows\System32\ftype.exe

To tell me which one is an executable, if Test-Path returns True then the file exists, else it returns False, which means the executable doesn't exist and is inbuilt function of cmd.exe.

I am sorry but I don't have access to a computer right now and for the next week, so I can't test my code, I am using an Android phone as I am writing this, because I am off in my maternal grandparents' house which is in countryside to spend the Chinese New Year, there aren't any computers where I am now.


I guess you only have one Python.exe present in $Env:Path because the result of (where.exe Python.exe)[0] is a [System.Char], and I guess its value is C, try to run:

(where.exe Python.exe)[0]

To tell whether I am right or not, this was unexpected but if there is only one line then it's a [System.String] object, if there are multiple lines then the result is an [Array], specifically [String[]], parentheses have multiple meanings in PowerShell, here it is used together with [0] which unambiguously means the first element in an [Array] (programs use 0 as beginning of indices rather than 1), so the thing inside the parentheses is treated as an [Array], because there is only one string it is converted into [System.Char] array [Char[]] to make it indexable.

2
  • I get this error. Commented Feb 10, 2021 at 20:27
  • @anoor1234 I have fixed my code, when you see this comment please try my updated code, it won't generate the errors you encountered. Commented Feb 10, 2021 at 23:57
1

Try these following steps:

  1. Create a shortcut for python.exe

  2. Change the shortcut target into something

    like d:\xxx...\python.exe (your_script.py)(your file path)

  3. Click "advance..." in the property panel of the shortcut, and click the option "run as administrator"

1
  • Did I do something wrong? What do you mean change the target to something, do I create a shortcut for python.exe, do you mean python.exe in C:\Users\My_User\AppData\Local\Programs\Python\Python38 or my script.py and then put in parenthesis in the target line after the path like what you said. Commented Feb 10, 2021 at 18:42

You must log in to answer this question.

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