Start a new topic

Handy Powershell to open virtual desktops for full screen connections

Thought I'd share this in case anyone else wants to do this.  Plus, maybe there's a better way? I just purchased RoyalTS, so I'm still learning.


I use buttons on my mouse to switch between virtual desktops. When RDP is in full screen mode, you have to do a special key combination (CTRL+ALT+HOME then CTRL+ALT+Left / Right)


So I have two buttons, one to go left, one to go right.

I wanted to make my connection go full screen with windows key passthrough on separate virtual desktops.


So I made a powershell script to accomplish this workflow. It takes two parameters:

desktopname and remove

desktopname is the name of the virtual desktop, to which I pass $Name%
remove is boolean, $true or $false

So you take the powershell script, and make two command tasks, one to open the virtual desktop and switch to it, the other to close it

You set the tasks on the connection or the folder, wait it to complete before connecting.  When you open a connection, powershell creates a virtual desktop with the connection name, switches to it, the returns to RoyalTS, which opens full screen.

When you disconnect, powershell removes the virtual desktop with the connection name.

It's not perfect, but it works well enough for me - and could probably be improved.

In the task, the command is the path to pwsh.exe on your machine. The working folder is the path to where you put the powershell script.


Arguments looks something like this: 

(to open)
 -WindowStyle Hidden -NoProfile -Command .\rtsVirtDesktop.ps1 -desktopname '$Name$'

(to close)
-WindowStyle Hidden -NoProfile -Command .\rtsVirtDesktop.ps1 -desktopname '$Name$' -remove $true


Note the quotes around $Name$, to handle spaces in connection name.

WindowStyle Hidden is ignored because RoyalTS opens a window anyway to run this. Not an issue.

The script is as follows:

param(

    [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$false)]

    [System.String]

    $desktopname="Desktop",

    [Parameter(Mandatory=$false, Position=1)] [string] $remove='false'

)

Import-Module -Name VirtualDesktop

if ($remove -eq $true)

{

    Remove-Desktop "$desktopname"

}

else

{

    New-Desktop | Switch-Desktop | Set-DesktopName -Name "$desktopname"

}

 

 

 

 


Of note, I'm using this module - I have no affiliation with it
PowerShell Gallery | VirtualDesktop 1.5.5

Ok - Made it much better. No powershell file needed. Just have to install the VirtualDesktop module into powershell. You can find it on the powershell gallery and github.

Then....


The open task is:


Command: "C:\Program Files\PowerShell\7\pwsh.exe"

Arguments: -WindowStyle Hidden -NoProfile -Command $dn = '$Name$'; $d=New-Desktop; Switch-Desktop $d; Set-DesktopName -Name $dn; Start-Sleep -Milliseconds 250;


The close task:


Command: "C:\Program Files\PowerShell\7\pwsh.exe"

Arguments: -WindowStyle Hidden -NoProfile -Command $dn = '$Name$'; Remove-Desktop $dn;


It's much faster, portable, and the 250ms delay is to ensure windows gets to the new desktop.


Assigning $dn isn't necessary, but I put it there so it's at the start of the command to make it easier to edit.

No need for a script or anything, and I think it'll work with powershell 5, but haven't tested it.

- HTHS

Login or Signup to post a comment