- 1 year ago
How do I wait for an Explorer window to finish opening?
Comment
1 / 1
You can wait for an Explorer window to finish opening by using the AppActivate
method from the WScript.Shell
object to bring the window to the foreground, and then use the SendKeys
method to send a command to the window.
Here is an example PowerShell script that waits for an Explorer window to finish opening:
powershell$folderPath = "C:\Path\To\Folder"
$wshell = New-Object -ComObject WScript.Shell
# Open the folder in a new Explorer window
$wshell.Run("explorer.exe `"$folderPath`"", 1)
# Wait for the Explorer window to finish opening
$windowTitle = Split-Path $folderPath -Leaf
while ($true) {
$wshell.AppActivate($windowTitle)
if ($wshell.AppActivate($windowTitle)) {
# Send the Enter key to the window to close it
$wshell.SendKeys("{ENTER}")
break
}
Start-Sleep -Milliseconds 100
}
This script opens the specified folder in a new Explorer window, waits for the window to finish opening by repeatedly activating it and checking if it's active, and then sends the Enter key to close the window.