「Windows PowerShell 磁碟機」(Windows PowerShell Drive) 是 Windows PowerShell 中可供存取的資料儲存位置,就像存取檔案系統磁碟機一樣。Windows PowerShell 提供者會建立一些磁碟機,例如檔案系統磁碟機 (包括 C: 和 D:)、登錄磁碟機 (HKCU: 和 HKLM:) 及憑證磁碟機 (Cert:),而您也可以自行建立 Windows PowerShell 磁碟機。這些磁碟機非常有用,但是只能從 Windows PowerShell 內部存取。使用其他 Windows 工具如 Windows 檔案總管或 Cmd.exe 均無法存取這些磁碟機。

Windows PowerShell 使用名詞 PSDrive 表示用於處理 Windows PowerShell 磁碟機的命令。如需取得目前工作階段的 Windows PowerShell 磁碟機清單,請使用 Get-PSDrive Cmdlet。

PS> Get-PSDrive

Name       Provider      Root                                   CurrentLocation
----       --------      ----                                   ---------------
A          FileSystem    A:\
Alias      Alias
C          FileSystem    C:\                                 ...And Settings\me
cert       Certificate   \
D          FileSystem    D:\
Env        Environment
Function   Function
HKCU       Registry      HKEY_CURRENT_USER
HKLM       Registry      HKEY_LOCAL_MACHINE
Variable   Variable

以上所示 Get-PSDrive 命令輸出的磁碟機可能與您系統上的磁碟機不同,但整份清單看來應該差不多。

檔案系統磁碟機是 Windows PowerShell 磁碟機的子集。Provider 欄內的 FileSystem 項目即代表檔案系統磁碟機 (在 Windows PowerShell 中,檔案系統磁碟機是由 Windows PowerShell FileSystem 提供者所支援)。

若要查看 Get-PSDrive Cmdlet 的語法,請輸入 Get-Command 命令加上 Syntax 參數:

PS> Get-Command -Name Get-PSDrive -Syntax
Get-PSDrive [[-Name] <String[]>] [-Scope <String>] [-PSProvider <String[]>] [-V
erbose] [-Debug] [-ErrorAction <ActionPreference>] [-ErrorVariable <String>] [-
OutVariable <String>] [-OutBuffer <Int32>] 

使用 PSProvider 參數可指定只顯示特定提供者支援的 Windows PowerShell 磁碟機。例如,若只要顯示 Windows PowerShell FileSystem 提供者支援的 Windows PowerShell 磁碟機,請輸入 Get-PSDrive 命令並將 PSProvider 參數指定為 FileSystem 值:

PS> Get-PSDrive -PSProvider FileSystem

Name       Provider      Root                                   CurrentLocation
----       --------      ----                                   ---------------
A          FileSystem    A:\
C          FileSystem    C:\                           ...nd Settings\PowerUser
D          FileSystem    D:\

若要檢視代表登錄 Hive 的 Windows PowerShell 磁碟機,請使用 PSProvider 參數指定只顯示 Windows PowerShell Registry 提供者支援的 Windows PowerShell 磁碟機:

PS> Get-PSDrive -PSProvider Registry

Name Provider Root CurrentLocation

---- -------- ---- ---------------

HKCU Registry HKEY_CURRENT_USER

HKLM Registry HKEY_LOCAL_MACHINE

您也可以使用標準位置 (Location) cmdlet 操控 Windows PowerShell 磁碟機:

PS> Set-Location HKLM:\SOFTWARE

PS> Push-Location .\Microsoft

PS> Get-Location

Path

----

HKLM:\SOFTWARE\Microsoft

新增 Windows PowerShell 磁碟機 (New-PSDrive)

您可以使用 New-PSDrive 命令自行新增 Windows PowerShell 磁碟機。若要查看 New-PSDrive 命令的語法,請輸入 Get-Command 命令加上 Syntax 參數:

PS> Get-Command -Name New-PSDrive -Syntax
New-PSDrive [-Name] <String> [-PSProvider] <String> [-Root] <String> [-Descript
ion <String>] [-Scope <String>] [-Credential <PSCredential>] [-Verbose] [-Debug
] [-ErrorAction <ActionPreference>] [-ErrorVariable <String>] [-OutVariable <St
ring>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]

建立新的 Windows PowerShell 磁碟機時必須提供三個參數:

  • 磁碟機的名稱 (可使用任何有效的 Windows PowerShell 名稱)

  • PSProvider (使用 "FileSystem" 代表檔案系統位置,"Registry" 代表登錄位置)

  • 根目錄,亦即新磁碟機根目錄的路徑

例如,您可以建立名稱為 "Office" 的磁碟機,將其對應到包含 Microsoft Office 應用程式的資料夾,例如 C:\Program Files\Microsoft Office\OFFICE11。若要建立該磁碟機,請輸入下列命令:

PS> New-PSDrive -Name Office -PSProvider FileSystem -Root "C:\Program Files\Micr
osoft Office\OFFICE11"

Name       Provider      Root                                   CurrentLocation
----       --------      ----                                   ---------------
Office     FileSystem    C:\Program Files\Microsoft Offic...
附註:

路徑通常不區分大小寫。

若要參照新的 Windows PowerShell 磁碟機,請輸入磁碟機名稱再加上冒號 (:),就像參照所有的 Windows PowerShell 磁碟機一樣。

Windows PowerShell 磁碟機有助於簡化許多工作。例如,Windows 登錄中有一些最重要的機碼路徑很長,以致存取不易且太難記住。關鍵的設定資訊位於 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion 下方。為了檢視和變更 CurrentVersion 登錄機碼中的項目,您可以建立 Windows PowerShell 磁碟機並將根目錄指定為該機碼,像這樣:

PS> New-PSDrive -Name cvkey -PSProvider Registry -Root HKLM\Software\Microsoft\W

indows\CurrentVersion

Name Provider Root CurrentLocation

---- -------- ---- ---------------

cvkey Registry HKLM\Software\Microsoft\Windows\...

接下來您可以變更位置為 cvkey: 磁碟機,就像切換到任何其他磁碟機一樣:

PS> cd cvkey:

或者:

PS> Set-Location cvkey: -PassThru

Path

----

cvkey:\

New-PsDrive Cmdlet 新增的磁碟機僅存留於目前的 Windows PowerShell 工作階段。一旦您關閉 Windows PowerShell 視窗,新磁碟機就會消失。若要儲存 Windows PowerShell 磁碟機,請使用 Export-Console Cmdlet 匯出目前的 Windows PowerShell 工作階段,然後使用 PowerShell.exe PSConsoleFile 參數加以匯入。或是將新磁碟機加到您的 Windows PowerShell 設定檔中。

刪除 Windows PowerShell 磁碟機 (Remove-PSDrive)

您可以使用 Remove-PSDrive Cmdlet 從 Windows PowerShell 刪除磁碟機。Remove-PSDrive Cmdlet 的用法很簡單,只要提供 Windows PowerShell 磁碟機名稱就能刪除特定的 Windows PowerShell 磁碟機。

例如,如果您已依照 New-PSDrive 主題介紹的方式新增了 Office: Windows PowerShell 磁碟機,便可輸入下列命令予以刪除:

PS> Remove-PSDrive -Name Office

若要刪除 cvkey: Windows PowerShell 磁碟機 (New-PSDrive 主題中亦有說明),請使用下列命令:

PS> Remove-PSDrive -Name cvkey

要刪除 Windows PowerShell 磁碟機很容易,但您無法刪除目前所在位置的磁碟機。例如:

PS> cd office:
PS Office:\> remove-psdrive -name office
Remove-PSDrive : Cannot remove drive 'Office' because it is in use.
At line:1 char:15
+ remove-psdrive  <<<< -name office

從 Windows PowerShell 外部新增與移除磁碟機

Windows PowerShell 會偵測 Windows 中新增或移除的檔案系統磁碟機,包括對應的網路磁碟機、附加的 USB 磁碟機,以及使用 net use 命令或由 Windows Script Host (WSH) 指令碼使用 WScript.Network MapNetworkDriveRemoveNetworkDrive 方法所刪除的磁碟機。




目錄