Run PowerShell Script As An Administrator


  1. Posts : 2,916
    Windows 10 Pro for the Bro
       #1

    Run PowerShell Script As An Administrator


    Hi. So I've been searching online on a way to do this yet I cannot find a way to do it.

    _______________________________________

    I need this in specific:

    I want to double click a PowerShell script file (which will run as an administrator) from Windows Explorer that has this as the script:
    Set-MpPreference -DisableRealtimeMonitoring $true

    (that script requires that PowerShell needs to be run as an administrator)
    _______________________________________

    So those are the restrictions:
    No right-click>run PowerShell as an administrator
    No opening a shortcut to PowerShell (as an administrator) and pasting the command


    Thanks
      My Computer


  2. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    1 Set the ps1 up as an elevated task in Task scheduler.
    - [you will want to set up the TS task to use highest privileges but not to store the password in order to avoid creating a security hole but I do not have any tasks in that state to copy here as examples]
    2 Create a shortcut that runs the TS task and double-click that instead of on the ps1 script file itself.
    - [the shortcut format would be C:\Windows\System32\schtasks.exe /Run /TN "Name of TS task"]

    Denis
      My Computer


  3. Posts : 14,046
    Windows 11 Pro X64 22H2 22621.1848
       #3
      My Computers


  4. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    pepanee,

    I thought @Ztruker's link was an interesting solution for your case. I have been having a bit of a play with it and it seems to be reliable.

    The linked article contained comments about a couple of apparent shortcomings -

    1 There is an unspecified problem when running a self-elevating script that lives on a networked drive.
    - I have tried it with my test script and it seems to be fine.

    2 The self-elevated script cannot pass arguments to another script.
    - I did not bother to test this. I think that the problem might well apply to any exchange of arguments between elevated & non-elevated scripts and might have nothing to do with this method of self-elevation.
    - Scripts can also exchange arguments by writing to / reading from files [amongst other things]. I do this all the time [because I run a PS script from MS Office VBA] and it is a perfectly workable approach.

    Denis
      My Computer


  5. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #5

    There is another way of doing it, see On how to run a .ps1 file as Administrator - TenForums

    In this proposed solution, an interim script is run that calls the originally-desired script. This does not seem, to me, to be as neat a solution because you would end up with two scripts to maintain.

    Denis
      My Computer


  6. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #6

    Of course, there is always the elevating shortcut method. This requires one extra step compared to setting up such a shortcut for a batch file.

    1 Create a shortcut to your PS1 file [by, for example, dragging it with the Alt key pressed]
    2 Right click on the shortcut & select Properties
    2.1 At the start of the target field, type in powershell.exe then a space to separate it from the script's path-name
    2.2 Click on the Advanced button & set the Run as Admin checkbox.
    2.3 OK, OK
    {you will notice the shortcut's icon change when you do this}

    You can now double-click on the shortcut instead of your original script and it will run as Admin [after the normal Admin challenge].

    The extra step compared to doing this for batch files is 2.1. Batch files can be set to run as Admin without having to job the Target field.

    Denis
      My Computer


  7. Posts : 2,916
    Windows 10 Pro for the Bro
    Thread Starter
       #7

    Okay thanks for the responses. I'll check out that info you guys mentioned.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 10:31.
Find Us




Windows 10 Forums