On how to run a .ps1 file as Administrator


  1. Posts : 66
    Windows 10 Pro 64 bit
       #1

    On how to run a .ps1 file as Administrator


    Hello everyone,

    If you have had a chance of going through the great Brink's Tutorial Apps - Uninstall in Windows 10 (I strongly advice you do), you may have noticed that, if you wish to automatize, at least to a certain extent, the procedure of getting rid of Windows 10 pre-installed bloatware (sorry, Apps would sound much better), you will inescapably bump into the need of creating a tailored .ps1 file.

    More specifically, you may want to copy and paste into a text editor like Notepad your customized set of instructions and then saving it with the extension .ps1 rather than .txt (or, which is the same, save it as a .txt file and then, disregarding Windows warnings, change its extension to .ps1)

    I hope an example might help. Let us assume I wish to uninstall, for instance, Alarms & Clock, Calendar and Mail, Candy Crush Soda Saga and Microsoft Solitaire Collection for all users. IMHO your file should be like this:

    Get-appxpackage -allusers *windowsalarms* | Remove-AppxPackage
    Get-appxpackage -allusers *windowscommunicationsapps* | Remove-AppxPackage
    Get-appxpackage -allusers *CandyCrushSodaSaga* | Remove-AppxPackage
    Get-appxpackage -allusers *solitairecollection* | Remove-AppxPackage


    So far so good, copy and paste all of the above into Notepad, save it as a .ps1 file and everybody's content. Apart from the fact that when you launch it with PowerShell, which is the final purpose of the whole iteration, it inevitably crashes due to the fact that, apparently, for these kind of instructions you need Administraor's privileges. That is, to be even more clear, those privileges you get when you right-click on PowerShell in Menu and select Run as Administrator.

    Which brings us to the question: how do I run a .ps1 file in PowerShell as Administrator?

    Kindly note that I am logged in my PC's as Administrator (single user, as a matter of fact).

    Thank you all in advance. :)
    Last edited by Lmbrt; 16 May 2016 at 13:00.
      My Computer


  2. Posts : 17,661
    Windows 10 Pro
       #2

    Add following command in your batch file to launch a PowerShell script elevated:

    Code:
    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""PS_Script_Path&Name.ps1""' -Verb RunAs}";
    As you cannot call an elevated PowerShell from command line, the command first starts normal PowerShell, which then in its turn calls another PowerShell process, this time elevated (the -Verb RunAs switch).

    This works :)

    Kari
      My Computer


  3. Posts : 66
    Windows 10 Pro 64 bit
    Thread Starter
       #3

    Kari said:
    Add following command in your batch file to launch a PowerShell script elevated:

    Code:
    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""PS_Script_Path&Name.ps1""' -Verb RunAs}";

    Thanks a lot for your answer! :)

    As I'm all thumbs, I am having problems "to set" the correct .ps1 file name...

    That is, if for instance my file is C:\Users\UserName\Desktop\-online.ps1, the command would be the following?

    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""PS_Script_C:\Users\UserName\Desktop\-online.ps1""' -Verb RunAs}";

    I know, that is a shameful question.

    Sorry to bother and thank you very much in advance.

    Lmbrt
      My Computer


  4. Posts : 17,661
    Windows 10 Pro
       #4

    Lmbrt said:
    That is, if for instance my file is C:\Users\UserName\Desktop\-online.ps1, the command would be the following?

    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""PS_Script_C:\Users\UserName\Desktop\-online.ps1""' -Verb RunAs}";
    No! The highlighted part is wrong, shouldn't be there.

    You replace the PS_Script_Path&Name.ps1 in my example with your PS script path and name, in this example of yours the command would be as follows:

    Code:
    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""C:\Users\UserName\Desktop\-online.ps1""' -Verb RunAs}";
      My Computer


  5. Posts : 66
    Windows 10 Pro 64 bit
    Thread Starter
       #5

    Kari said:
    No! The highlighted part is wrong, shouldn't be there.

    You replace the PS_Script_Path&Name.ps1 in my example with your PS script path and name, in this example of yours the command would be as follows:

    Code:
    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""C:\Users\UserName\Desktop\-online.ps1""' -Verb RunAs}";

    As I said, I'm all thumbs.

    Thank you very much indeed!

    I'll try and I will keep you posted...
      My Computer


  6. Posts : 1
    Windows
       #6

    just a question guys:
    How can I run this script below using the Windows Credentials ? "-credential $credential" (for example):
    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""C:\Users\UserName\Desktop\-online.ps1""' -Verb RunAs}";
      My Computer


  7. Posts : 68,894
    64-bit Windows 11 Pro for Workstations
       #7
      My Computers


 

  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 19:26.
Find Us




Windows 10 Forums