Is there a way to switch user from powershell or cmd?

11,665

Do you search for something like this?

POWERSHELL CODE:

$Username = 'UserX'
$Password = 'P@ssW0rd'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force #Not recommended, but if rights are set on that location you can use it like this, otherwise encrypt it (recommended).
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

#You can use it like this, or use it with other commands and ' -Credential ...' 
Invoke-Command -ComputerName "DeviceName" -Credential $Cred -ScriptBlock {#Your Code} 
Share:
11,665

Related videos on Youtube

Gevorg Hakobyan
Author by

Gevorg Hakobyan

Updated on September 18, 2022

Comments

  • Gevorg Hakobyan
    Gevorg Hakobyan over 1 year

    I have a Windows 10 machine that auto-logins the Administrator account using netplwiz to load some scripts at startup and I need to automatically sign in with another user after the Administrator account is signed in.

    • Is there a way to do this from powershell or cmd?
    • Gevorg Hakobyan
      Gevorg Hakobyan about 5 years
      Script run nodejs aplication, which start and control other application. I want to use this trick , to split my windows server, because that application cant run as multiple process.
    • user2531336
      user2531336 about 5 years
      Why do you login automatically with administrator to run a script ? This ticks all the boxes of an "unwise solution" to a problem. You claim it is for nodejs, that does NOT need a Windows session (as in, Explorer and the gui running), so logging on as Administrator is not required!
    • Gevorg Hakobyan
      Gevorg Hakobyan about 5 years
      Yes, but nodejs application is control other windows application (GUI application) , so for that i need to login to gui interface, and there is some specific env variables for every user.
    • postanote
      postanote about 5 years
      only one use can be interactively logged on to any Windows box. Sure you can run a PowerShell script as another user, but that requires that user to enter creds. There is no skirting Windows security boundaries with PowerShell. well, not without hacking. You other option to run stuff as another user is using MS SysInternals psexec or use a logon script or scheduled task.