在 Windows PowerShell 中,Windows PowerShell 驱动器是一种数据存储位置,您可以像访问文件系统驱动器一样对其进行访问。Windows PowerShell 提供程序会为您创建一些驱动器,例如文件系统驱动器(包括 C: 和 D:)、注册表驱动器(HKCU: 和 HKLM:)和证书驱动器 (Cert:),您也可以创建自己的 Windows PowerShell 驱动器。这些驱动器十分有用,但它们仅在 Windows PowerShell 中可用。无法使用其他 Windows 工具(例如,Windows 资源管理器或 Cmd.exe)来访问这些驱动器。

Windows PowerShell 使用名词 PSDrive 来表示用于 Windows PowerShell 驱动器的命令。若要获得 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 的语法,请键入带 Syntax 参数的 Get-Command 命令:

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 驱动器,请键入带 PSProvider 参数和 FileSystem 值的 Get-PSDrive 命令:

PS> Get-PSDrive -PSProvider FileSystem

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

若要查看表示注册表配置单元的 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

还可以将标准的位置 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 命令的语法,请输入带 Syntax 参数的 Get-Command 命令:

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 驱动器名称。

例如,如果已经添加了 Office: Windows PowerShell 驱动器,如 New-PSDrive 主题中所示,可以通过键入以下命令来删除该驱动器:

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 方法删除的驱动器。




目录