可以通过使用 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 驱动器,你必须提供三个参数:

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

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

PS> New-PSDrive -Name Office -PSProvider FileSystem -Root "C:\Program Files\Microsoft 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 C:\Users\maxsu> New-PSDrive -Name cvkey -PSProvider Registry -Root HKLM\Software\Microsoft\Windows\CurrentVersion

Name           Used (GB)     Free (GB) 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 配置文件中。