Automatically Open Multiple File Explorer Windows with Custom Positions and Sizes
As an expert in technology, I often find myself needing to open multiple instances of the File Explorer on my Windows machine. This can be a tedious task, especially when trying to position and size each window on the screen.
To solve this problem, I created a PowerShell script that automates the process of opening multiple File Explorer windows with custom positions and sizes. This script uses the Shell.Application COM object to interact with the File Explorer and move/resize the windows as needed.
Here’s how it works:
Step 1: Create a new file with a .ps1 extension in your favorite text editor.
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Win {
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
}
"@
Step 2: Define an array of folder locations and window properties (Top, Left, Width, Height).
$windows = @(
@{Location="C:\"; Top=0; Left=100; Width=800; Height=600},
@{Location="D:\"; Top=200; Left=300; Width=800; Height=600},
@{Location="E:\"; Top=300; Left=500; Width=800; Height=600}
)
Step 3: Create a new instance of the Shell.Application COM object.
$shell = New-Object -ComObject Shell.Application
Step 4: Loop through each location and its window properties, opening the folder and moving/ resizing the window as needed.
foreach ($window in $windows) {
# Open the folder
$shell.Open($window.Location)
# Wait briefly to ensure the window has opened
Start-Sleep -Seconds 2
# Get the collection of open windows from the Shell.Application object
$openWindows = $shell.Windows()
# Loop through each open window and check for the correct folder path
foreach ($explorerWindow in $openWindows) {
try {
if ($explorerWindow.Document.Folder.Self.Path -eq $window.Location) {
# Get the handle (hWnd) of the Explorer window
$hWnd = $explorerWindow.HWND
# Move and resize the window
[Win]::MoveWindow($hWnd, $window.Left, $window.Top, $window.Width, $window.Height, $true)
Write-Host "Moved window for $($window.Location) to position [$($window.Left), $($window.Top)] with size [$($window.Width)x$($window.Height)]"
break
}
} catch {
# If there's an error accessing the Document or Folder, skip this window
Write-Host "Error accessing window for $($window.Location). Skipping..."
}
}
}
Step 5: Run the script whenever you need to open multiple File Explorer windows with custom positions and sizes. Simply modify the array of folder locations and window properties as needed.
Note: This script is available on my GitHub page, where you can find more useful scripts for automating Windows tasks.
By using this PowerShell script, you’ll save time and effort when working with multiple File Explorer windows on your Windows machine. Happy automating!