如何用PowerShell查找并执行指定.exe文件?
Absolutely, you can pipe Get-ChildItem directly to Start-Process—PowerShell natively handles file system objects passed through the pipeline. But you’ll want to add safeguards to avoid launching multiple copies (if the .exe exists in multiple locations) and handle cases where the file can’t be found. Here’s how to do it effectively:
Basic Pipeline Usage
This recursively searches a directory for your target .exe and launches the first match:
Get-ChildItem -Path "C:\Program Files" -Filter "YourApp.exe" -Recurse -File | Select-Object -First 1 | Start-Process
Select-Object -First 1prevents accidental multiple launches if duplicates exist.-Fileensures we only target files, not directories.
Robust Deployment-Friendly Script
For asset management use cases, add error handling and optimize the search to avoid slow full-system scans:
# Define likely install paths to speed up the search $searchPaths = @("C:\Program Files", "C:\Program Files (x86)", "$env:USERPROFILE\AppData\Local") # Find the target executable $targetExe = Get-ChildItem -Path $searchPaths -Filter "YourApp.exe" -Recurse -File -ErrorAction SilentlyContinue | Select-Object -First 1 if ($targetExe) { Write-Host "Launching $($targetExe.FullName)" # Add -Wait if you need the script to pause until the app closes (optional for deployment) Start-Process -FilePath $targetExe.FullName } else { Write-Error "YourApp.exe not found on this device." # Add logging here for your asset management tool to track failures }
- Restricting search paths to common install locations cuts down on scan time and avoids security warnings from accessing restricted system folders.
-ErrorAction SilentlyContinuesuppresses irrelevant access-denied errors for protected directories.
Alternative: Faster Wildcard Search (If Partial Path Is Known)
If you know the .exe lives in a subfolder under a known parent, skip full recursion with wildcards:
$targetExe = Get-Item -Path "C:\Program Files\YourAppFolder*\YourApp.exe" -ErrorAction SilentlyContinue | Select-Object -First 1 if ($targetExe) { Start-Process $targetExe }
This is quicker than scanning every subdirectory and works well if the app’s install structure is consistent.
Key Deployment Tips
- Avoid C:\ root scans: They’re slow and often trigger endpoint security tools.
- Cover 32/64-bit systems: Include both
Program Filesdirectories to account for different app architectures. - Log results: Add logging commands to report success/failure back to your asset management platform for visibility.
内容的提问来源于stack exchange,提问作者Fynn

