3

I want to run a small, unobtrusive script in the background, in the form of a .bat file; and would like it to run in desktop 2 in Windows 10.

The bat file is loaded via Task Scheduler at boot. Can I program the desktop preference, within the .bat file itself?

1 Answers1

0

In command, create a .bat file with the location of the ps1 file below. There's no way you can start a ps1 file from boot, but you can start a bat file from boot.

Copy the code below, and save it in the ".ps1" file you have within the .bat boot file.

If ran by Powershell, it'll create an extra desktop, and execute notepad on each desktop. Modify the program if you don't want a new desktop every time it runs, remove the 'notepad' locations if you don't want notepad (or your program) to boot in both desktops; and replace the remaining notepad by the program you want to run.

Window location and dimension should be saved on closing, so their relative position should be the same as last time you used them.. (I think) You will also need to download the'virtualDesktop.ps1' file in the same directory, from here: https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5

. "$PSScriptRoot\VirtualDesktop.ps1"

"Number of virtual desktops: $(Get-DesktopCount)"
Sleep 1

"Create a new desktop:"
$NewDesktop = New-Desktop
$OldDesktop = Get-CurrentDesktop

"Number of virtual desktops: $(Get-DesktopCount)"
"Number of new desktop: $(Get-DesktopIndex($NewDesktop))"

Sleep 1

"Switch to new desktop, start notepad there and switch back"

Sleep 1

$NewDesktop | Switch-Desktop
notepad
Sleep 1
Switch-Desktop $OldDesktop
Sleep 1

"Move notepad to current desktop"
(ps notepad)[0].MainWindowHandle | Move-Window (Get-CurrentDesktop) | Out-Null

Sleep 1

"Move powershell window to new desktop and switch to it"
$NewDesktop | Move-Window (Get-ConsoleHandle) | Switch-Desktop

Sleep 1

"Pin notepad window to all desktops"
Pin-Window ((Get-Process "notepad")[0].MainWindowHandle)

Sleep 1

"Remove original desktop"
Remove-Desktop $OldDesktop

"Number of virtual desktops: $(Get-DesktopCount)"

Sleep 1