Skip to content

Instantly share code, notes, and snippets.

@josephan
Last active February 3, 2024 06:54
Show Gist options
  • Save josephan/cd399794dac11d619f56175d9332f307 to your computer and use it in GitHub Desktop.
Save josephan/cd399794dac11d619f56175d9332f307 to your computer and use it in GitHub Desktop.
Windows 10: From the start menu, focus Google Chrome if it's open otherwise open a new window.

Problem

Out of habit from using Ubuntu, to focus Google Chrome I hit the Start Menu button and type "ch" (which autocompletes to Chrome) and hit enter to open Chrome. This is faster than using Alt+Tab and using mental energy trying to find Chrome window. However in Windows 10 this always opens a new window and I'm left with a bunch of windows open and a cluttered desktop.

Goal

To mimic the behaviour of opening Google Chrome from macOS's Spotlight or Ubuntu's start menu where if you already have a Google Chrome instance, it will focus the open window rather than create a new window. Windows 10 by default always open a new Chrome window from the start menu.

Instructions

  1. Create a Windows Powershell Script in any directory called chrome.ps1. Example: C:\Users\<USERNAME>\Documents\chrome.ps1
  2. Copy the contents of the chrome.ps1 file in this gist to your new file.
  3. Remove Google Chrome from your Start Menu.
  4. Create a Windows Powershell shortcut to your Start Menu. Powershell is located in C:\Windows\System32\WindowsPowerShell\v1.0. The start menu folder is located in C:\ProgramData\Microsoft\Windows\Start Menu\Programs.
  5. Rename the Powershell shortcut to chrome.
  6. Right click the Powershell shortcut named chrome and open properties.
  7. In the Shortcut tab:
    • edit the Target property to C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -ExecutionPolicy Bypass -File "C:\Users\<USERNAME>\Documents\chrome.ps1" (make sure to use the correct directory where your powershell script is located).
    • edit the Run property to Minimized
  8. Save the property settings. Done!

Hit the Start Menu button, type "ch" and open your "chrome" powershell script shortcut. You may have to manually click it at first to make it rank higher on your Menu index.

function Show-Window {
param(
[Parameter(Mandatory)]
[string] $ProcessName
)
# As a courtesy, strip '.exe' from the name, if present.
$ProcessName = $ProcessName -replace '\.exe$'
# Get the PID of the first instance of a process with the given name
# that has a non-empty window title.
# NOTE: If multiple instances have visible windows, it is undefined
# which one is returned.
$hWnd = (Get-Process -ErrorAction Ignore $ProcessName).Where({ $_.MainWindowTitle }, 'First').MainWindowHandle
if (-not $hWnd) { Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" }
$type = Add-Type -PassThru -NameSpace Util -Name SetFgWin -MemberDefinition @'
[DllImport("user32.dll", SetLastError=true)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError=true)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError=true)]
public static extern bool IsIconic(IntPtr hWnd); // Is the window minimized?
'@
# Note:
# * This can still fail, because the window could have bee closed since
# the title was obtained.
# * If the target window is currently minimized, it gets the *focus*, but its
# *not restored*.
$null = $type::SetForegroundWindow($hWnd)
# If the window is minimized, restore it.
# Note: We don't call ShowWindow() *unconditionally*, because doing so would
# restore a currently *maximized* window instead of activating it in its current state.
if ($type::IsIconic($hwnd)) {
$type::ShowWindow($hwnd, 9) # SW_RESTORE
}
}
# Sample invocation
Show-Window chrome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment