在我需要從 Powershell 快速切換到 Windows Explorer 的情況下,我曾經能夠從我碰巧所在的任何目錄啟動 Windows Explorer,如下所示:
PS > explorer .
(這真的只是打電話C:\Windows\explorer.exe。)
在升級到 Windows 11 之前它運行良好。現在它默默地無法做任何事情。
我已經確認,即使在使用新的終端應用程式時,該命令仍然可以在命令提示符中運行。而在 pwsh 中,explorer別名仍然指向C:\Windows\explorer.exe.
那么為什么它現在在 Powershell 中被破壞了呢?有什么解決方法嗎?
PS:我已經確認以下內容也不起作用:
PS > & explorer . # Nothing
PS > C:\Windows\explorer.exe . # Nada
PS > & C:\Windows\explorer.exe . # Zilch
uj5u.com熱心網友回復:
這只是一種解決方法,但是當您確認它有效時,我會將其轉換為答案:
$shell = New-Object -ComObject Shell.Application
$shell.Open( $PWD.ProviderPath )
# Also works:
# $shell.Explore( $PWD.ProviderPath )
創建ShellCOM 物件的實體,然后呼叫其方法Open或在資源管理器Explore中打開從自動變數獲得的當前目錄$PWD。
正如mklement0指出的那樣,我們必須使用$PWD.ProviderPathas $PWD.Pathmay be based on a PowerShell-only drive,檔案資源管理器等外部程式不知道。
您可能還需要檢查提供程式的型別,以確保僅針對檔案系統路徑呼叫上述代碼:
# As a demonstration, let $PWD point to a registry location
Set-Location HKCU:\Software
# The following if branch won't be entered
if( $PWD.Provider.Name -eq 'FileSystem' ) { ... }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/487086.html
