Here is RunAsAdmin.ps1.
It's working for me on Win11+PS5.1. Give it a go and let me know if something doesn't work.
<#
.SYNOPSIS
This script allows a user that is not a member of the administrators group to run
a process as a different account that DOES have admin rights. The userid and password
are saved in the registry in base64 format. This won't stop a tech saavy user
who can write Powershell code from getting the password, but for the majority
of users, this is good enough.
If additional security is needed, one option would be to use the PS2EXE tool to encapsulate
the script into an executable file. https://github.com/MScholtes/PS2EXE
Author: MotoX80 on Microsoft Q&A Forums
.DESCRIPTION
Run a process as a UAC elevated admin account.
This script accepts these parameters.
-Define A switch to indicate that a task is to be defined and saved into the registry.
-Run A switch to indicate that a task is to be launched.
-Task The name of the "thing" we want define/run.
-User The userid of the administrator account
-Password The userid's password.
-Exe The path to the program to be executed.
-Parms Optional, any parameters to passed to the program.
.EXAMPLE
./RunAsAdmin.ps1 -Define -Task xxxx -Exe cmd.exe -Parms "/k timeout /t 5" -User admin -Password admin
./RunAsAdmin.ps1 -Run -Task xxxx
.EXAMPLE
./RunAsAdmin.ps1 -Define -Task AR -Exe "C:\Program Files\Mythicsoft\Agent Ransack\AgentRansack.exe" -User admin -Password admin
./RunAsAdmin.ps1 -Run -Task AR
#>
param (
[switch]$Run,
[switch]$Define,
[string]$User = "",
[string]$Password = "",
[string]$Exe = "",
[string]$Parms = "",
[string]$Task = ""
)
$RegPath = 'HKCU:\SOFTWARE\Madware\Tasks' # modify if you don't like my name
If ($Task -eq "") {
"You need to specify a task name."
return
}
if ($Run) {
"Attempting to to run $Task"
if (!(Test-Path –Path "$RegPath\$Task")) {
"Task not defined."
return
}
$x64 = (Get-ItemProperty –Path "$RegPath\$Task").1
$User = [Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($x64))
$x64 = (Get-ItemProperty –Path "$RegPath\$Task").2
$Password = [Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($x64))
$x64 = (Get-ItemProperty –Path "$RegPath\$Task").3
$Exe = "'" + [Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($x64)) + "'"
$x64 = (Get-ItemProperty –Path "$RegPath\$Task").4
$Parms = "'" + [Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($x64)) + "'"
$secpswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($User, $secpswd)
# We first have to start a PS process with the users' credentials.
# That instance of PS can then launch the elevated application process.
# The downside to this approach is that if the app .exe doesn't run, this current script won't capture the error.
try {
if ($Parms -eq "''") {
$p2 = "Start-Process $Exe -verb runas"
Start-Process Powershell.exe -ArgumentList "-command $p2" -credential $Cred
} else {
$p2 = "Start-Process $Exe -argumentlist $Parms -verb runas"
Start-Process Powershell.exe -ArgumentList "-command $p2" -credential $Cred
}
} catch {
"Error starting Powershell."
$_.exception
}
} elseif ($Define) {
if ($User -eq "") {
"Please specify a user."
return
}
if ($Password -eq "") {
"Please specify a password."
return
}
if ($Exe -eq "") {
"Please specify an executable."
return
}
if (!(Test-Path –Path "$RegPath\$Task")) {
"Adding new task $Task."
$null = New-Item –Path "$RegPath\$Task" –Force
} else {
"Updating task $Task."
}
# Check to see if the exe exists. If the user entered "cmd.exe" which would be resolved via the
# system path, just assume it's ok.
# If they've included a drive letter, check that.
if ($exe.contains(":") -eq $true) {
if (!(Test-Path –Path $exe)) {
"File not found. $exe"
return
}
}
$x64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($User))
$null = New-ItemProperty –Path "$RegPath\$Task" –Name '1' –Value $x64 –PropertyType 'String' -Force
$x64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($Password))
$null = New-ItemProperty –Path "$RegPath\$Task" –Name '2' –Value $x64 –PropertyType 'String' -Force
$x64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($Exe))
$null = New-ItemProperty –Path "$RegPath\$Task" –Name '3' –Value $x64 –PropertyType 'String' -Force
$x64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($Parms))
$null = New-ItemProperty –Path "$RegPath\$Task" –Name '4' –Value $x64 –PropertyType 'String' -Force
} else {
"You need to specify either -Define or -Run."
}