Launching script as different user from another script

I have written a script that is used to download the current version of another script and look for and install any missing dependant modules. I am posting the last line only. I will post the rest of the script “over 100 lines” if necessary. Everything works fine except the last line.
Background:
All of the users of this script are admins on their local computer with their standard user account but have a different elevated network ADM account that is needed to access and manage user/device accounts in Active Directory and AzureAD, so just the simple “runas” will not work

Using this line does exactly what it needs to except the credential dialog has the mydomain\ populated in the username box, requiring the user to double or triple click in the field to enter their login ID
Start-Process -FilePath Powershell "C:\Foldername\mainscript.ps1" -Credential mydomain\

Using this line will do everything perfectly except launch the final script.
Start-Process -FilePath Powershell "C:\Foldername\mainscript.ps1" -verb runasuser

Thank you in advance.

Dan,
Welcome to the forum. :wave:t4:

If I got it right … depending on the naming conventions of the accounts and admin accounts in your AD you could use the account name of the user and transform it to the admin name of the user and us it for the -Credential parameter.

How often will this “other” script change? How often will the modules change? How about using a software deployment solution instead? If these “admin” users need to enter their admin credentials anyway - why not starting the PowerShell session already elevated with their admin account?

The reason for not just starting Powershell as adm is in order for that to work you would have to right-click on the icon and run as different user in Win10 and in Win 11 you would have to choose more options and after that click on run as different user. This way I create a shortcut that reads as follows

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe “c:\folder\filenametorun.ps1”

Then filenametorun.ps1 does the downloads and asks for the ADM creds then launches the main script.

I think I figured it out. I changed the function to get the current user and add -ADM to the end

$usr = (Get-WmiObject  –Class Win32_ComputerSystem | Select-Object UserName).Username
Expand-Archive -LiteralPath '\\ServerName\FolderName\SubfolderName\Current_Version_Main_Script.zip' -DestinationPath C:\Local_Folder -Force
$Form.Close()
Start-Process -FilePath Powershell "C:\Folder\filenametorun.ps1" -Credential $usr-ADM

That’s what I meant. :man_shrugging:t4:

You should not use Get-WmiObject anymore. Use its successor instead:

An easier way of getting the user name of the currently logged on user on Windows systems is to use the environment variable USERNAME like this:

$Env:USERNAME