How to automatically run application form as admin

This forum can be browsed by the general public. Posting is no longer allowed as the product has been discontinued.
This topic is 10 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
bjorn80
Posts: 5
Last visit: Wed Jun 05, 2019 11:57 pm

How to automatically run application form as admin

Post by bjorn80 »

Hi
Ive created a sharepoint tool gui, and id love to be able to start it as administrator when people double click the .exe file.

I allready have the code to start form as admin:
PowerShell Code
Double-click the code block to select all.
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
However, i cant find where to place it so that it will be the first line in the file.

If i first export my project to a .ps1 file and add the above code on the top and run it in powershell it works fine, starting the program as an admin. But where should i place this code in PowerShell Studio 2012?

best regards Bjorn
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: How to automatically run application form as admin

Post by Alexander Riedel »

If this is a product related question please post in the corresponding product forum.
If you are posting a general PowerShell question please post in the PowerShell forum.
This forum is for technical support for our community tools only.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 10 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.