I have a small powershell script (script.ps1
file) that installs a service.
Remove-Service -Name "MyService"
This code has 2 problems:
- It should run with admininstrator privilegs
New-Service
command is a powershell6 command, but ps1 files runs by ps5.
I found here a solution for the first problem:
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
}
These code opens a new instance of powershell with admin privilegs, But it opens a ps5.
How can i open a ps6/ps7? if I cange it to Start-Process pwsh
, the parameter -Verb runAs
make an error.
Sorry for my bad english, and thank you for any help.