0

I'm trying to write a Powershell script that automates setting up my work environment each day. To do this, I have the following two scripts.

Set-Window.ps1

Function Set-Window {
<#
.LINK
https://superuser.com/questions/1324007/setting-window-size-and-position-in-powershell-5-and-6
#>
[cmdletbinding(DefaultParameterSetName='Name')]
Param (
 [parameter(
 Mandatory=$False,
 ValueFromPipelineByPropertyName=$True, 
 ParameterSetName='Name'
 )]
 [string]$ProcessName='*',
 [int]$X,
 [int]$Y,
 [int]$Width,
 [int]$Height,
 [switch]$HideWindow
)
Begin {
 Try { 
 [void][Window]
 } Catch {
 Add-Type @"
 using System;
 using System.Runtime.InteropServices;
 public class Window {
 [DllImport("user32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 public extern static bool MoveWindow( 
 IntPtr handle, int x, int y, int width, int height, bool redraw);
 [DllImport("user32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 public extern static bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
 }
"@
 }
}
Process {
 If ($PSBoundParameters.ContainsKey('ProcessName')) {
 $Processes = Get-Process -Name "$ProcessName"
 } else {
 throw 'No processes match criteria specified'
 }
 If ($PSBoundParameters.ContainsKey('HideWindow')) {
 $Processes | ForEach-Object {
 # 0 is the value that represents "hide".
 # see https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/show-or-hide-windows
 # for more details
 [Window]::ShowWindowAsync($_.MainWindowHandle, 0)
 }
 } else {
 $Processes | ForEach-Object {
 [Window]::MoveWindow($_.MainWindowHandle, $X, $Y, $Width, $Height, $True)
 }
 }
}
}

Start-Work.ps1

. C:\Users\<username>\Projects\Personal\PowerShell\Set-Window.ps1
# Start all necessary applications
Start-Process "C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\Code.exe" '--log=off'
Start-Process Chrome '--profile-directory="Profile 2"'
Start-Process "C:\Users\<username>\AppData\Roaming\Spotify\Spotify.exe"
Start-Process "C:\Users\<username>\AppData\Roaming\Zoom\bin\Zoom.exe"
Start-Process "C:\Users\<username>\AppData\Local\slack\slack.exe"
# Some applications can be moved right away, but still best to wait a bit
Start-Sleep -Seconds 1
Set-Window -ProcessName Spotify -X 400 -Y 0 -Height 600 -Width 1200
Set-Window -ProcessName Chrome -X 200 -Y 0 -Height 600 -Width 1200
Set-Window -ProcessName Slack -X 600 -Y 0 -Height 600 -Width 1200
# Others need a more time to load everything
Start-Sleep -Seconds 3
Set-Window -ProcessName Code -X 0 -Y 0 -Height 600 -Width 1200
Set-Window -ProcessName Zoom -HideWindow
Start-Sleep -Milliseconds 500
Exit

To run these scripts, I created a shortcut with the target set to C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy remotesigned -File C:\Users\<username>\Projects\Personal\PowerShell\Start-Work.ps1, and I pinned that shortcut to my taskbar. When I click the shortcut, everything runs as expected with one exception: the PowerShell program never closes. Based on the output that is occasionally being written to the console, I assume this is because of VSCode. I've been able to demonstrate this by running a simple .ps1 script that only opens VS Code and can observe that it opens a PowerShell window. This doesn't happen, though, if I simply run Start-Process Code from an open PowerShell Window.

So, with all of that being said, does anyone know how I can force the PowerShell window to actually close?

asked Feb 13, 2022 at 17:42
4
  • stackoverflow.com/questions/42222004/run-exe-in-background Commented Feb 13, 2022 at 17:45
  • @mashuptwice - That seems to just hide VSCode and keep it running in the background, which isn't what I want. What I want is for the PowerShell window that launches when I run the shortcut I created to Exit when it reaches the Exit line of Start-Work.ps1, but instead it just sits there listening. And if I close the window manually, it also closes VS Code. Commented Feb 13, 2022 at 17:54
  • Interesting, I thought Start-Job behaves like daemonizing a process with & in linux. Commented Feb 13, 2022 at 18:39
  • Start-Job or Start-Process seem to be the way to go as mentioned here: stackoverflow.com/questions/63531764/… and here: stackoverflow.com/questions/185575/… Commented Feb 13, 2022 at 18:43

1 Answer 1

0

It seems like VS Code might be keeping the standard output stream open. I had the same behavior as you, but I was able to get powershell to close by redirecting the output to a file instead:

Start-Process "C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\Code.exe" '--log=off' `
 -RedirectStandardOutput "C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\output.log"
exit

it does create a file, but the file is empty - I never had any output being written to the console though.

answered Feb 14, 2022 at 15:42
1
  • This seems to have solved the issue. Thank you! Commented Feb 17, 2022 at 4:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.