I understand that this can easily be accomplished by using alt-tab, but the main purpose of this script's creation is to teach myself some PowerShell basics.
I'm writing a script that, when run, toggles the foreground window between powershell and the current foreground window. I read this question and used one of the answers to get the code for retrieving the current foreground window but it doesn't seem to grab the correct window - it instead seems to grab explorer.exe
Below is my code for the script with hopefully helpful comments:
# Toggle-PowerShell.ps1
# Toggles focus between powershell and the current active window.
# If this script isn't run with -NoProfile, it will switch focus to itself.
. $PSScriptRoot\..\Functions\Toggle-Window.ps1
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Util {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"@
$a = [util]::GetForegroundWindow()
# Get-Unique may be unnecessary here, but including it for the case when
# working with Chrome as the stored process
$storedProcess=get-process | ? { $_.mainwindowhandle -eq $a } | Get-Unique
If(Test-Path $PSScriptRoot\Toggle-PowerShell.temp)
{
$tempArray=(Get-Content $PSScriptRoot\Toggle-Powershell.temp)
# the id number is at index three of tempArray
Show-Process -Process (Get-Process -id $tempArray[3])
# delete the file so the next time we run the script it toggles to PS
Remove-Item $PSScriptRoot\Toggle-PowerShell.temp
} Else
{
$propertiesFile=$PSScriptRoot\..\currentSession.properties
$propertiesMap = convertfrom-stringdata (get-content $propertiesfile -raw)
Show-Process -Process (Get-Process -id $propertiesMap.'PowerShellPID')
# write a new temp file that contains the stored process's id
# so that the next time this script is run it toggles back
$storedProcess | Select-Object Id > $PSScriptRoot\Toggle-PowerShell.temp
}
I was thinking perhaps that maybe I should try getting the active window instead of the foreground window but another question's answer said that foreground implies active.
I'm on Windows 10.
EDIT: I think that it may be using explorer.exe as the 'foreground window' because I call the script via a shortcut, which launches from explorer.