I have a Powershell VB script
There is no such thing as a "Powershell VB script". There are Powershell scripts, with a .ps1 file extension, and VB scripts, with a .vbs file extension. I am going to assume that you are not doing anything with VB.
Here is the script that I tested with on Win10/PS5.1. It demonstrates how to test to see if the user/script is running elevated so that it doesn't execute cmdlets that will fail due to not being an admin.
start-transcript -path c:\temp\Test3.log
Add-Type -AssemblyName System.Windows.Forms
$msgs = @()
function LogIt($msg){
$script:msgs += $msg
$msg
}
"Test3.ps1 starting."
Logit ("Running on {0} as user {1}" -f $env:COMPUTERNAME, $(whoami.exe))
Logit ("Execution policy is {0} " -f (get-executionpolicy))
$admin = (whoami.exe /groups | select-string S-1-5-32-544 ) -ne $null
if ($admin) {
Logit "You are a member of the Administrators group."
$Elevated = (whoami.exe /all | select-string S-1-16-12288) -ne $null
if ($Elevated){
Logit 'This process is running elevated.'
###############################
##### Add admin commands here.
###############################
} else {
Logit 'But this process is not running elevated. '
Logit "If you encounter errors, try with 'Run as administrator'"
}
} else {
Logit "You do not have admin access."
}
$z = [System.Windows.Forms.MessageBox]::Show($msgs -join "`n")
"Test3.ps1 ending."
stop-transcript
I defined the scheduled task to run as the Builtin\Users group and set the trigger to the "Log on of any user".
When any user logs on, I see a Powershell window briefly pop up and then go away due to using -Windowstyle Hidden.
When a non-admin logs on, it shows this msgbox.
Depending on the "Run with highest privileges" check box, an admin gets one of these.
If you get different results, check the transcript log file and see if you have a typo or something else.