my task is to create new windows local user, log in, using it and then do some actions. Creating new user wasn't a problem but i don't know how to switch current user to new one.
What i did is a piece of script which start new powershell window using new user:
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($config_name, $secpasswd)
Start-Process powershell.exe -Credential $mycreds -NoNewWindow
Is is possible to start doing rest of the script in this new window??
When you start a PowerShell script, do it as here: Run PowerShell (or ps.exe) and add the script file with -file <script. ps1> as argument. Then, add the desired credentials by clicking the Change User or Group button.
Then you can right-click on the PowerShell icon, which shows you an option as Windows PowerShell. Hovering over that options, Click Shift and right-click together to open another menu. You can choose Run as Different User from the new menu. Then a different popup would be opened, as shown in the below image.
Open Command Prompt or PowerShell in your Window 10 PC. Now, type in “tsdiscon” (without quotes) and press 'Enter'. The 'tsdiscon' command will take you to the lock screen. From there, you can go through the usual process to log into any user account on the PC.
If you like command-line environments, open Command Prompt or PowerShell. Then, type tsdiscon and press Enter. The tsdiscon command takes you to the Lock screen. On it, click, tap, or press a key on your keyboard and then choose switch users.
Simple way is following:
Start-Process powershell.exe -Credential $mycreds -NoNewWindow -ArgumentList "-noexit -command FULL_PATH_TO_SCRIPT\init.ps1"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With