Using PSexec to launch PowerShell session as system with specific window attributes

19,309

Solution 1

I figured it out. This is what I did:

1) Created 2 bat files in a folder called C:\Launchers--one for launching CMD as system and one for PS

2) Downloaded the latest version of SysinternalsSuite and placed the folder in C:\

3) Pinned a shortcut to my taskbar and then did the following: A) set the shortcut "Target:" field to: C:\Windows\System32\cmd.exe /C "C:\Launchers\Launch_PS_As_Sys.bat" B) set "Start In:" field to "C:\Launchers". C) clicked "Advanced" and checked "Run as administrator"

The bat file contains this line for the PowerShell System launcher:

C:\SysinternalsSuite\psexec.exe /dis powershell.exe -NoExit -Command "& {cd C:\Users\<yourusername>\Desktop; $HOST.UI.RawUI.ForeGroundColor='Cyan'}"

And this line for CMD Prompt:

C:\SysinternalsSuite\psexec.exe /dis cmd.exe /k "cd C:\Users\<yourusername>\Desktop & Color 0A"

It turns out that the way that the .bat and/or PSExec parse quotes somehow conflict with each other so that you have to use single quotes instead of double quotes. Probably my mistake. Anyway I hope someone finds this useful.

Solution 2

Two things:

  1. You can use Powershell profiles to customize the shell
  2. You can simply change the shell properties (color, font size, buffer) - they persist after all

Both works without having to use external tools like psexec.

Your approach is overly complicated. Just create a shortcut to Powershell, then go open the link's properties and set the "Start in" value to the directory you want to start Powershell with and customize the shell colors via the colors tab. The same works with cmd.exe.

Share:
19,309

Related videos on Youtube

montag
Author by

montag

Updated on September 18, 2022

Comments

  • montag
    montag almost 2 years

    I'm trying to launch a PowerShell session via PSexec with alternate colors and some window attributes. The reason why I would need to run PowerShell through PSExec is so that I can have a ready-made one-click shortcut to launch a terminal window in the Local System context. This makes sense for testing scripts and code within MDT/SCCM packages on a daily basis. It sounds like a pointless thing to do but I have a lot of command windows open so it's hard for me to distinguish which terminal windows is running as system, user, local admin, etc. The colors would make it easier to see this difference immediately.

    So far I'm trying this, but explorer crashes upon launch:

    C:\SysinternalsSuite\psexec.exe /i /s "PowerShell.exe -NoExit -Command "& $HOST.UI.RawUI.ForeGroundColor = cyan"
    

    I can launch PowerShell just fine, like this:

    C:\SysinternalsSuite\psexec.exe /i /s powershell.exe
    

    I'm sure it has something to do with the way the quotes are being parsed.

    • montag
      montag over 9 years
      I could probably tell PSExec to run a script as system that would define the window attributes as well, but I'm not sure which would be easier.
    • Tim Ferrill
      Tim Ferrill over 9 years
      Your path/quotes don't look right. Are you getting any error messages you can share?
    • montag
      montag over 9 years
      I know the path is right. It really doesn't matter in this case with the quotes in the path, but for a folder like Program Files you would need it. I will update my question to elaborate a little bit.