管理 Windows PowerShell 驱动器Managing Windows PowerShell Drives

本文内容

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

Windows PowerShell 可针对适用于 Windows PowerShell 驱动器的命令使用名词 PSDrive。有关 Windows PowerShell 会话中的 Windows PowerShell 驱动器列表,请使用 Get-PSDrive cmdlet。

1. PS> Get-PSDrive

3. Name       Provider      Root                                   CurrentLocation
4. ----       --------      ----                                   ---------------
5. A          FileSystem    A:\
6. Alias      Alias
7. C          FileSystem    C:\                                 ...And Settings\me
8. cert       Certificate   \
9. D          FileSystem    D:\
10. Env        Environment
11. Function   Function
12. HKCU       Registry      HKEY_CURRENT_USER
13. HKLM       Registry      HKEY_LOCAL_MACHINE
14. Variable   Variable

尽管显示内容中的驱动器与你的系统上的驱动器有所不同,但是该列表将看起来类似于 Get-PSDrive 命令的输出(如上所示)。

文件系统驱动器是 Windows PowerShell 驱动器的子集。你可以通过 Provider 列中的 FileSystem 条目标识文件系统驱动器。(Windows PowerShell 中的文件系统驱动器受 Windows PowerShell FileSystem 提供程序支持。)

若要查看 Get-PSDrive cmdlet 的语法,请使用 Syntax 参数键入 Get-Command 命令:

1. PS> Get-Command -Name Get-PSDrive -Syntax

3. Get-PSDrive [[-Name] <String[]>] [-Scope <String>] [-PSProvider <String[]>] [-V
4. erbose] [-Debug] [-ErrorAction <ActionPreference>] [-ErrorVariable <String>] [-
5. OutVariable <String>] [-OutBuffer <Int32>]

PSProvider 参数允许你仅显示受特定提供程序支持的 Windows PowerShell 驱动器。例如,若要仅显示受 Windows PowerShell FileSystem 提供程序支持的 Windows PowerShell 驱动器,请使用 PSProvider 参数和 FileSystem 值键入 Get-PSDrive 命令:

1. PS> Get-PSDrive -PSProvider FileSystem

3. Name       Provider      Root                                   CurrentLocation
4. ----       --------      ----                                   ---------------
5. A          FileSystem    A:\
6. C          FileSystem    C:\                           ...nd Settings\PowerUser
7. D          FileSystem    D:\

若要查看表示注册表配置单元的 Windows PowerShell 驱动器,请使用 PSProvider 参数来仅显示受 Windows PowerShell Registry 提供程序支持的 Windows PowerShell 驱动器:

1. PS> Get-PSDrive -PSProvider Registry

3. Name       Provider      Root                                   CurrentLocation
4. ----       --------      ----                                   ---------------
5. HKCU       Registry      HKEY_CURRENT_USER
6. HKLM       Registry      HKEY_LOCAL_MACHINE

你还可以将标准 Location cmdlet 与 Windows PowerShell 驱动器结合使用:

1. PS> Set-Location HKLM:\SOFTWARE
2. PS> Push-Location .\Microsoft
3. PS> Get-Location

5. Path
6. ----
7. HKLM:\SOFTWARE\Microsoft

添加新的 Windows PowerShell 驱动器 (New-PSDrive)Adding New Windows PowerShell Drives (New-PSDrive)

你可以通过使用 New-PSDrive 命令添加自己的 Windows PowerShell 驱动器。若要获取 New-PSDrive 命令的语法,请使用 Syntax 参数输入 Get-Command 命令:

1. PS> Get-Command -Name New-PSDrive -Syntax

3. New-PSDrive [-Name] <String> [-PSProvider] <String> [-Root] <String> [-Descript
4. ion <String>] [-Scope <String>] [-Credential <PSCredential>] [-Verbose] [-Debug
5. ] [-ErrorAction <ActionPreference>] [-ErrorVariable <String>] [-OutVariable <St
6. ring>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]

若要创建一个新的 Windows PowerShell 驱动器,你必须提供三个参数:

  • 驱动器的名称(可使用任何有效的 Windows PowerShell 名称)
  • PSProvider(将“FileSystem”用于文件系统位置,将“Registry”用于注册表位置)
  • 根,即指向新驱动器的根目录的路径

例如,可以创建一个名为“Office”的驱动器,它将映射到包含你的计算机上的 Microsoft Office 应用程序的文件夹,例如 C:\Program Files\Microsoft Office\OFFICE11。若要创建该驱动器,请键入以下命令:

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

4. Name       Provider      Root                                   CurrentLocation
5. ----       --------      ----                                   ---------------
6. Office     FileSystem    C:\Program Files\Microsoft Offic...

备注

一般情况下,路径不区分大小写。

在执行所有 Windows PowerShell 驱动器时,请参考新的 Windows PowerShell 驱动器,格式是在名称后面跟一个冒号 (:)。

Windows PowerShell 驱动器可以使许多任务变得更简单。例如,Windows 注册表中的某些最重要的项的路径长度非常长,难以访问且难以记住这些路径。关键的配置信息位于 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion。若要查看和更改 CurrentVersion 注册表项中的项,你可以创建一个其根在该项中的 Windows PowerShell 驱动器,方法是键入:

1. PS> New-PSDrive -Name cvkey -PSProvider Registry -Root HKLM\Software\Microsoft\W
2. indows\CurrentVersion

4. Name       Provider      Root                                   CurrentLocation
5. ----       --------      ----                                   ---------------
6. cvkey      Registry      HKLM\Software\Microsoft\Windows\...

然后,你可以像对任何其他驱动器一样,将位置更改为 cvkey: 驱动器:

PS> cd cvkey:

或者:

1. PS> Set-Location cvkey: -PassThru

3. Path
4. ----
5. cvkey:\

New-PsDrive cmdlet 仅将新的驱动器添加到当前 Windows PowerShell 会话中。如果关闭 Windows PowerShell 窗口,则会丢失新的驱动器。若要保存 Windows PowerShell 驱动器,请使用 Export-Console cmdlet 导出当前 Windows PowerShell 会话,然后使用 PowerShell.exe PSConsoleFile 参数来将其导入。或者,将新的驱动器添加到 Windows PowerShell 配置文件中。

删除 Windows PowerShell 驱动器 (Remove-PSDrive)Deleting Windows PowerShell Drives (Remove-PSDrive)

你可以通过使用 Remove-PSDrive cmdlet 从 Windows PowerShell 中删除驱动器。Remove-PSDrive cmdlet 易于使用;若要删除特定 Windows PowerShell 驱动器,只需提供 Windows PowerShell 驱动器名称。

例如,如果你添加了 Office:Windows PowerShell 驱动器(如 New-PSDrive 主题中所示),则可以通过键入以下内容将其删除:

1. Remove-PSDrive -Name Office

若要删除cvkey: Windows PowerShell 驱动器,同样中, 所示New-psdrive主题中,使用以下命令:

1. Remove-PSDrive -Name cvkey

可以轻松删除 Windows PowerShell 驱动器,但是如果你位于该驱动器中,则无法删除它。例如:

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

添加和删除 Windows PowerShell 之外的驱动器Adding and Removing Drives Outside Windows PowerShell

Windows PowerShell 检测在 Windows 中添加或删除的文件系统驱动器,包括映射的网络驱动器、附加的 USB 驱动器,以及通过使用 net use 命令或来自 Windows 脚本宿主 (WSH) 脚本的 WScript.NetworkMapNetworkDrive 和 RemoveNetworkDrive 方法删除的驱动器。