Windows 10: On how to run a .ps1 file as Administrator

  1.    15 May 2016 #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 ComputerSystem Spec

  2. Kari's Avatar
    Posts : 15,011
    Windows 10 Pro
       15 May 2016 #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 ComputerSystem Spec

  3.    23 May 2016 #3

    Kari said: View Post
    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 ComputerSystem Spec

  4. Kari's Avatar
    Posts : 15,011
    Windows 10 Pro
       23 May 2016 #4

    Lmbrt said: View Post
    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 ComputerSystem Spec

  5.    23 May 2016 #5

    Kari said: View Post
    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 ComputerSystem Spec

  6.    24 Oct 2016 #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 ComputerSystem Spec

  7. Brink's Avatar
    Posts : 33,045
    64-bit Windows 10 Pro build 18252
       24 Oct 2016 #7
      My ComputersSystem Spec


 

Related Threads
What I don't understand is why with administrator rights, I can't access or delete files or even save files to the Windows folder. I also don't have the option if I right click to run File Explorer with Administrator rights. 29268 If I...
There seems to be a permanent "Run as administrator" entry in my right-click context menu. If I click on this for anything but an executable or shortcut to an executable, Windows 10 throws the below error, even if I do have a program associated...
When I launch a recent folder or any folder linked on the pinned taskbar icon for File Explorer it launches what appears to cmd.exe with "Administrator" and the folder path in the top bar. the folder is also displayed in the command prompt itself....
How to Add 'Run as administrator' to MSI File Context Menu in Windows 10 Some legitimate applications require a full administrator access token (elevated) to perform their functions or tasks. For example, when a program that you are trying to run...
How to Add 'Run as administrator' to VBS File Context Menu in Windows 10 Some legitimate applications require a full administrator access token (elevated) to perform their functions or tasks. For example, when a program that you are trying to run...
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 20:16.
Find Us