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(&quot;user32.dll&quot;)]
        [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 &quot;hide&quot;.
        # 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&lt;username>\AppData\Local\Programs\Microsoft VS Code\Code.exe" '--log=off' Start-Process Chrome '--profile-directory="Profile 2"' Start-Process "C:\Users&lt;username>\AppData\Roaming\Spotify\Spotify.exe" Start-Process "C:\Users&lt;username>\AppData\Roaming\Zoom\bin\Zoom.exe" Start-Process "C:\Users&lt;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?

1 Answers1

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.

Cpt.Whale
  • 10,914