vendredi 30 octobre 2020

Execute a powershell script as Administrator (from inside the script)

 Add the following lines at the beginning of your script :


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

}