1.    15 May 2016 #1
    Join Date : Apr 2016
    Florence, Italy
    Posts : 51
    Windows 10 Pro 64 bit

    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.    15 May 2016 #2
    Join Date : Oct 2013
    A Finnish expat in Germany
    Posts : 13,103
    Windows 10 Pro

    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
    Join Date : Apr 2016
    Florence, Italy
    Posts : 51
    Windows 10 Pro 64 bit
    Thread Starter

    Quote Originally Posted by Kari 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.    23 May 2016 #4
    Join Date : Oct 2013
    A Finnish expat in Germany
    Posts : 13,103
    Windows 10 Pro

    Quote Originally Posted by Lmbrt 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
    Join Date : Apr 2016
    Florence, Italy
    Posts : 51
    Windows 10 Pro 64 bit
    Thread Starter

    Quote Originally Posted by Kari 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
    Join Date : Oct 2016
    Posts : 1
    Windows

    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.    24 Oct 2016 #7
    Join Date : Oct 2013
    Posts : 25,705
    64-bit Windows 10 Pro build 17046
      My ComputersSystem Spec

 


Similar Threads
Thread Forum
How to run File Explorer as Administrator
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...
General Support
Run as administrator -> This file does not have a program associated..
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...
General Support
File Explorer launches Administrator command prompt
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....
General Support
Customization Add Run as administrator to Context Menu of MSI Files in Windows 10
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...
Tutorials
Customization Add Run as administrator to Context Menu of VBS Files in Windows 10
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...
Tutorials
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:51.
Find Us
Twitter Facebook Google+ Ten Forums iOS App Ten Forums Android App



Windows 10 Forums