Skip to content

Instantly share code, notes, and snippets.

@lkishfy
Last active August 30, 2018 19:10
Show Gist options
  • Select an option

  • Save lkishfy/1c033cf32d42670e1205a0dfb8e1efd0 to your computer and use it in GitHub Desktop.

Select an option

Save lkishfy/1c033cf32d42670e1205a0dfb8e1efd0 to your computer and use it in GitHub Desktop.
Start Virtual Machine on Boot (Also shows how to run a .ps1 when elevated permissions are necessary)
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""C:\Users\Lou\Desktop\startVM.ps1""' -Verb RunAs}";
@lkishfy
Copy link
Copy Markdown
Author

lkishfy commented Aug 30, 2018

...basically you're using Powershell to run Powershell. The second instance is elevated to Admin and is running your script with those permissions.

Full explanation:

Start-Process can be used to run a program, and also has the parameter -Verb RunAs which elevates the program to run as Admin.

We can't call Start-Process from a batch file as it's a PowerShell command.

But we can run powershell from a batch file, then using the -command parameter to run Start-Process.

We use Start-Process to run powershell (again) and run your script when it is elevated to Admin: -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment