The simplest way to run the script as Administrator by default is to directly configure the shortcut to "Run as Administrator":
Right-click the shortcut and select
"Properties"
On the "Shortcut" tab, click the "Advanced" button"
Select the "Run as administrator" option and then click
"OK" and "OK" again
If you prefer to change the execution privilege directly in the script code, you can do so as described below:
# Obtém o ID do usuário
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
# Obtém informações do grupo ao qual o usuário pertence
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Obtém informações sobre o grupo "Administrador"
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Verifica se a execução atual já está em modo elevado
# Essa verificação é necessária para que o script não entre em looping
if (-Not ($myWindowsPrincipal.IsInRole($adminRole)))
{
# Cria um novo processo do PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Informa como parâmetro para o novo processo, o caminho do script atual
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Parâmetro para o novo processo que solicita a
# execução em modo elevado (Administrador)
$newProcess.Verb = "runas";
# Inicia o novo processo
[System.Diagnostics.Process]::Start($newProcess);
# Encerra o processo atual (que está em modo Usuário)
exit
}
# Aqui, você coloca o código que será executado pelo seu script em modo Administrador
if((Get-Service -Name 'MSSQL$SQLEXDEV').Status -eq "Running")
{
Stop-Service -Name 'MSSQL$SQLEXDEV'
Set-Service -Name 'MSSQL$SQLEXDEV' -StartupType Disabled
}
else
{
Set-Service -Name 'MSSQL$SQLEXDEV' -StartupType Manual
Restart-Service -Name 'MSSQL$SQLEXDEV'
}
Source: A self elevating Powershell script - by Benjamin Armstrong
According to the parameter documentation -Executionpolicy:
This parameter determines the Powershell execution policy for the machine, for a user, or for a session.
The rules defined for the Powershell execution policy aim to help the user not to execute scripts by "mistake" or "carelessness".
These rules are not security policies and do not prevent the user from executing a particular code differently from the rules defined in the Executionpolicy.
Simply put, there are six main policies:
Restricted - does not allow script execution
Allsigned - allows only the execution of digitally signed scripts
Remotesigned - allows the execution of locally developed unsigned scripts. If the script has been downloaded (remote), it must have a digital signature
Unrestricted - allows the execution of unsigned scripts, but warns the user if the script has been downloaded
Bypass - runs any script without any warning
Undefined - undefined policy. Powershell searches for a standard user or machine policy. If all are undefined, the default is "Restricted"
I recommend consulting the documentation of this parameter for more details, because there are several possible configuration strategies.