79583150

Date: 2025-04-20 07:58:25
Score: 1
Natty:
Report link

Use a temporary VBS script to refocus your PowerShell window after launching the process

Start-Process -FilePath "MyProcessPath"

Start-Sleep -Seconds 1

$myWindowTitle = (Get-Process -Id $PID).MainWindowTitle

$tempPath = [System.IO.Path]::GetTempFileName().Replace(".tmp", ".vbs")

$code = @"

Set WshShell = WScript.CreateObject("WScript.Shell")

WScript.Sleep 500

WshShell.AppActivate "$myWindowTitle"

"@

$code | Out-File $tempPath -Encoding ASCII

Start-Process "wscript.exe" $tempPath -Wait

Remove-Item $tempPath

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: masoud