Run Powershell As Different User

Posted on admin
Run Powershell As Different User
  1. Run Powershell Script As Another User

I'm currently working on a CI/CD pipeline for our TFS-enviroment. I'm totally new with this, since i'm an apprentice in a other. I want to be able to run the script1.ps1 from withing script.ps1 as another user and with elevated privileges but I cannot make it work. The new powershell window opens but closes immediately. Here is the script. I have a powershell script that does various automation for my server, and I am trying to simply run a command as an Administrator user instead of the default local service user. I can't seem to figure out how to call a command as an Administrator without having to have any user interaction. You cannot locally run a command as another user.

If you want to schedule a task to run at a certain time, you should really be using the task scheduler, which you can then specify 'Run as' as one of the options. There is also a RUNAS switch /SAVECRED which will save the credentials after the first time you enter them. Other than that, the only other way I am aware of to run programs as another user is to use an external tool (such as PsExec - which is a SysInternals tool available freely on TechNet) or PowerShell directly which supports secure strings.–Aug 11 '14 at 16:18. If you plan on running a batch file that calls a PowerShell script in a scheduled task, you can use the security options of the task scheduler. That's how I would handle running as a different user.As for calling a PowerShell script from a batch file I usually do something like this: @ECHO OFFpowershell.exe -executionpolicy unrestricted -File 'C:ScriptsDo-Something.ps1'You can get all the command line arguments from powershell /?You can put that in a.bat file and run that as the scheduled task's action; or you can put powershell.exe as the action and give it the arguments of -executionpolicy unrestricted -File 'C:ScriptsDo-Something.ps1' in the scheduled task. I prefer the latter because I have one less script file to play with.

Jrx1216 wrote:I usually just shift+right click 'launch as a different user' on either the script or on my powershell shortcutThis is what I want to work. When I right click on the script, I am not given that option.I can right click on Powershell and run it as a different user, then paste the script there.And Neally, yeah, that may work, would the commands you listed be part of the scheduled task or would it be at the top of the script?Ideally (and I fully recognize that my ideals may not match my realities), I would want the script to be able to be just right clicked, run with Powershell and have me enter my elevated password and that be the end of it. Jrx1216 wrote:Wizkid wrote:Jrx1216 wrote:I usually just shift+right click 'launch as a different user' on either the script or on my powershell shortcutThis is what I want to work. When I right click on the script, I am not given that option.I can right click on Powershell and run it as a different user, then paste the script there.And Neally, yeah, that may work, would the commands you listed be part of the scheduled task or would it be at the top of the script?Ideally (and I fully recognize that my ideals may not match my realities), I would want the script to be able to be just right clicked, run with Powershell and have me enter my elevated password and that be the end of it.Are you just doing a regular right click or Shift+right click? It works fine for me on windows 10 1803 and Server 2016 with Shift+right clickShift+Right Click, the context menu for PS1 files are not showing Run as different user. Wizkid wrote:Jrx1216 wrote:Wizkid wrote:Jrx1216 wrote:I usually just shift+right click 'launch as a different user' on either the script or on my powershell shortcutThis is what I want to work. When I right click on the script, I am not given that option.I can right click on Powershell and run it as a different user, then paste the script there.And Neally, yeah, that may work, would the commands you listed be part of the scheduled task or would it be at the top of the script?Ideally (and I fully recognize that my ideals may not match my realities), I would want the script to be able to be just right clicked, run with Powershell and have me enter my elevated password and that be the end of it.Are you just doing a regular right click or Shift+right click?

It works fine for me on windows 10 1803 and Server 2016 with Shift+right clickShift+Right Click, the context menu for PS1 files are not showing Run as different user.Yeah, sorry, I deleted my post after I tried that on another system and it didn't work. Not sure what we did to make that work on ours. Download navicat premium 64 bit. If I figure it out, I'll post here.

Wizkid wrote:Evan7191 wrote:Similar to Jrx1216's suggestion, you can make a custom shortcut to Powershell that specifies the -File parameter and the path to your script. Then you can right-click on the shortcut and select Run as different user.I've tried that as well and it doesn't show up in the context menu.Which OS and version are you using? It works for me on Windows 10 Pro 1703, 1709, 1803, and 1809.EDIT: This is the Shift+Right-click menu. My previous response said right-click menu by mistake. Evan7191 wrote:Wizkid wrote:Evan7191 wrote:Similar to Jrx1216's suggestion, you can make a custom shortcut to Powershell that specifies the -File parameter and the path to your script. Then you can right-click on the shortcut and select Run as different user.I've tried that as well and it doesn't show up in the context menu.Which OS and version are you using? It works for me on Windows 10 Pro 1703, 1709, 1803, and 1809.EDIT: This is the Shift+Right-click menu.

Powershell

My previous response said right-click menu by mistake.1803 here for me. Yeah, I even made sure to put the shortcut on the desktop (to make sure it wasn't a network drive/share issue).I am guessing that there is some sort of group policy here that disables that for Powershell shortcuts. It works for other kinds of shortcuts but for some reason, not Powershell.

Run Powershell Script As Another User

Powershell Add-Type -AssemblyName System. Forms $FileBrowser = New-Object System. OpenFileDialog $FileBrowser. InitialDirectory = 'fileserverHomeDrivesmyaccountPowershell Scripts' $FileBrowser. ShowDialog $filepath = $FileBrowser. FileName $arguments = '-executionpolicy bypass -file ' $fullArgs = ' + $arguments + '$filepath' + ' Start-process powershell.