如何通过PowerShell处理剪贴板中QR Code解码路径并在Windows Explorer打开?
Absolutely, you can easily handle this with PowerShell. Here are a few practical approaches tailored to your needs:
One-Liner (Quick On-the-Fly Use)
If you just need to run this once or occasionally, open PowerShell and paste this command:
Get-Clipboard | Where-Object { Test-Path $_ } | ForEach-Object { Start-Process explorer.exe -ArgumentList "`"$_`"" }
Breakdown of what it does:
Get-Clipboard: Pulls the file path directly from your clipboardWhere-Object { Test-Path $_ }: Filters out invalid paths (so Explorer doesn’t open to a default location if your clipboard has non-path content)Start-Process explorer.exe -ArgumentList ""$_"": Launches Windows Explorer with the valid path, wrapping it in quotes to handle spaces in folder/file names.
Reusable Script with Validation
For a more robust, reusable solution, save this as a .ps1 file (e.g., OpenClipboardPath.ps1):
$clipboardContent = Get-Clipboard # Check if clipboard is empty if ([string]::IsNullOrWhiteSpace($clipboardContent)) { Write-Warning "Clipboard is empty! Please copy a file path first." exit 1 } # Verify the path exists if (Test-Path -Path $clipboardContent) { # Open the path in Explorer, handle spaces safely Start-Process -FilePath explorer.exe -ArgumentList "`"$clipboardContent`"" Write-Host "Successfully opened: $clipboardContent" } else { Write-Warning "The clipboard content (`"$clipboardContent`") is not a valid file or directory path." }
To run the script:
- Open PowerShell
- Navigate to the folder where you saved it (e.g.,
cd C:\YourScriptsFolder) - Execute it with
.\OpenClipboardPath.ps1
One-Click Desktop Shortcut
For frequent use without opening PowerShell every time:
- Right-click your desktop > New > Shortcut
- In the location field, enter:
(Replace the path with where you saved your script)powershell.exe -ExecutionPolicy Bypass -File "C:\Full\Path\To\Your\OpenClipboardPath.ps1" - Name the shortcut something like "Open Clipboard Path in Explorer" and click Finish
Now you can double-click this shortcut anytime you need to launch Explorer with the path from your clipboard.
Note: If you hit execution policy errors, run Set-ExecutionPolicy RemoteSigned in an elevated PowerShell window (this only needs to be done once to allow running local scripts).
内容的提问来源于stack exchange,提问作者ManuelKetisch

