管理 Windows PowerShell 驱动器Managing Windows PowerShell Drives

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

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

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

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

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

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

    1. PS> Get-PSDrive -PSProvider FileSystem
    2. Name Provider Root CurrentLocation
    3. ---- -------- ---- ---------------
    4. A FileSystem A:\
    5. C FileSystem C:\ ...nd Settings\PowerUser
    6. D FileSystem D:\

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

    1. ---- -------- ---- ---------------
    2. HKCU Registry HKEY_CURRENT_USER
    3. HKLM Registry HKEY_LOCAL_MACHINE

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

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

    1. PS> Get-Command -Name New-PSDrive -Syntax
    2. New-PSDrive [-Name] <String> [-PSProvider] <String> [-Root] <String> [-Descript
    3. ion <String>] [-Scope <String>] [-Credential <PSCredential>] [-Verbose] [-Debug
    4. ] [-ErrorAction <ActionPreference>] [-ErrorVariable <String>] [-OutVariable <St
    5. ring>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]
    • 驱动器的名称(可使用任何有效的 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"
    3. Name Provider Root CurrentLocation
    4. 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
    3. Name Provider Root CurrentLocation
    4. ---- -------- ---- ---------------
    5. cvkey Registry HKLM\Software\Microsoft\Windows\...

    PS> cd cvkey:

    或者:

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

    你可以通过使用 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. + remove-psdrive <<<< -name office

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