# Launching script as different user from another script

**URL:** <https://forums.powershell.org/t/launching-script-as-different-user-from-another-script/21789>\
**Category:** PowerShell Help\
**Created:** [March 2, 2023, 12:53pm UTC](https://forums.powershell.org/t/launching-script-as-different-user-from-another-script/21789 "2023-03-02T12:53:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PCMedicJAX](https://avatars.discourse-cdn.com/v4/letter/p/e9bcb4/32.png) [@PCMedicJAX](https://forums.powershell.org/u/PCMedicJAX)\
**Post date:** [March 2, 2023, 12:53pm UTC](https://forums.powershell.org/t/launching-script-as-different-user-from-another-script/21789/1 "2023-03-02T12:53:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 2, 2023, 4:19pm UTC](https://forums.powershell.org/t/launching-script-as-different-user-from-another-script/21789/2 "2023-03-02T16:19:41Z")

</div>

Dan,  
Welcome to the forum. 👋🏽

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.

> [@PCMedicJAX](#):
>
> a script that is used to download the current version of another script and look for and install any missing dependant modules.

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?

---

<div class="post-metadata">

**Author:** ![PCMedicJAX](https://avatars.discourse-cdn.com/v4/letter/p/e9bcb4/32.png) [@PCMedicJAX](https://forums.powershell.org/u/PCMedicJAX)\
**Post date:** [March 2, 2023, 8:51pm UTC](https://forums.powershell.org/t/launching-script-as-different-user-from-another-script/21789/3 "2023-03-02T20:51:42Z")

</div>

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

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 2, 2023, 9:19pm UTC](https://forums.powershell.org/t/launching-script-as-different-user-from-another-script/21789/4 "2023-03-02T21:19:32Z")

</div>

> [@PCMedicJAX](#):
>
> I changed the function to get the current user and add -ADM to the end

That’s what I meant. 🤷🏽‍♂️

> [@PCMedicJAX](#):
>
> ```auto
> $usr = (Get-WmiObject –Class Win32_ComputerSystem | Select-Object UserName).Username
> 
> ```

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

> **[Get-CimInstance (CimCmdlets) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.3)**
>
> This cmdlet is only available on the Windows platform. The Get-CimInstance cmdlet gets the CIM instances of a class from a CIM server. You can specify either the class name or a query for this cmdlet. This cmdlet returns one or more CIM instance...

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:

```auto
$Env:USERNAME

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:15pm UTC](https://forums.powershell.org/t/launching-script-as-different-user-from-another-script/21789/5 "2024-05-16T20:15:04Z")

</div>


