当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 获取 PSDrive (Microsoft.PowerShell.管理)

作者:精品下载站 日期:2024-12-14 02:12:13 浏览:13 分类:玩电脑

获取 PSDrive (Microsoft.PowerShell.管理)


获取 PSDrive

模块 :Microsoft.PowerShell.Management

获取当前会话中的驱动器。

句法

Get-PSDrive
   [[-Name] <String[]>]
   [-Scope <String>]
   [-PSProvider <String[]>]
   [<CommonParameters>]
Get-PSDrive
   [-LiteralName] <String[]>
   [-Scope <String>]
   [-PSProvider <String[]>]
   [<CommonParameters>]

描述

Get-PSDrive cmdlet 获取当前会话中的驱动器。您可以获取会话中的特定驱动器或所有驱动器。

此 cmdlet 获取以下类型的驱动器:

  • 计算机上的 Windows 逻辑驱动器,包括映射到网络共享的驱动器。
  • PowerShell 提供程序公开的驱动器(例如,Certificate:、Function: 和 Alias: 驱动器)以及 Windows PowerShell 注册表提供程序公开的 HKLM: 和 HKCU: 驱动器。
  • 使用 New-PSDrive cmdlet 创建的会话指定的临时驱动器和持久映射网络驱动器。

从 Windows PowerShell 3.0 开始,New-PSDrive cmdlet 的 Persist 参数可以创建保存在本地计算机上并可在其他会话中使用的映射网络驱动器。有关详细信息,请参阅New-PSDrive。

此外,从 Windows PowerShell 3.0 开始,当外部驱动器连接到计算机时,Windows PowerShell 会自动将 PSDrive 添加到代表新驱动器的文件系统中。您无需重新启动 Windows PowerShell。同样,当外部驱动器与计算机断开连接时,Windows PowerShell 会自动删除代表已删除驱动器的 PSDrive。

示例

示例 1:获取当前会话中的驱动器

PS C:\> Get-PSDrive

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
Alias                                  Alias
C                 202.06      23718.91 FileSystem    C:\
Cert                                   Certificate   \
D                1211.06     123642.32 FileSystem    D:\
Env                                    Environment
Function                               Function
HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE
Variable                               Variable

此命令获取当前会话中的驱动器。

输出显示硬盘驱动器 (C:)、CD-ROM 驱动器 (D:) 以及 Windows PowerShell 提供程序公开的驱动器(Alias:、Cert:、Env:、Function:、HKCU:、HKLM: 和 Variable :)。

示例 2:获取计算机上的驱动器

PS C:\foo> Get-PSDrive D

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
D                1211.06     123642.32 FileSystem    D:\

此命令获取计算机上的 D: 驱动器。请注意,命令中的驱动器号后面没有冒号。

示例 3:获取 Windows PowerShell 文件系统提供程序支持的所有驱动器

PS C:\> Get-PSDrive -PSProvider FileSystem
Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
A                                                    A:\
C                 202.06      23718.91 FileSystem    C:\
D                1211.06     123642.32 FileSystem    D:\
G                 202.06        710.91 FileSystem    \Music\GratefulDead

此命令获取 Windows PowerShell 文件系统提供程序支持的所有驱动器。这包括固定驱动器、逻辑分区、映射网络驱动器以及使用 New-PSDrive cmdlet 创建的临时驱动器。

示例 4:检查驱动器是否正在用作 Windows PowerShell 驱动器名称

if (Get-PSDrive X -ErrorAction SilentlyContinue) {
    Write-Host 'The X: drive is already in use.'
} else {
    New-PSDrive -Name X -PSProvider Registry -Root HKLM:\SOFTWARE
}

此命令检查 X 驱动器是否已用作 Windows PowerShell 驱动器名称。如果不是,该命令将使用 New-PSDrive cmdlet 创建映射到 HKLM:\SOFTWARE 注册表项的临时驱动器。

示例 5:比较文件系统驱动器的类型

PS C:\> Get-PSDrive -PSProvider FileSystem
Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
A                                                    A:\
C                 202.06      23718.91 FileSystem    C:\
D                1211.06     123642.32 FileSystem    D:\
G                 202.06        710.91 FileSystem    \Music\GratefulDead
X                                      Registry      HKLM:\Network

PS C:\> net use
New connections will be remembered.
Status       Local     Remote                    Network
-------------------------------------------------------------------------------
OK           G:        \Server01\Public         Microsoft Windows Network

PS C:\> [System.IO.DriveInfo]::GetDrives() | Format-Table
Name DriveType DriveFormat IsReady AvailableFreeSpace TotalFreeSpace TotalSize     RootDirectory VolumeLabel
---- --------- ----------- ------- ------------------ -------------- ---------     ------------- -----------
A:\    Network               False                                                 A:\
C:\      Fixed NTFS          True  771920580608       771920580608   988877418496  C:\           Windows
D:\      Fixed NTFS          True  689684144128       689684144128   1990045179904 D:\           Big Drive
E:\      CDRom               False                                                 E:\
G:\    Network NTFS          True      69120000           69120000       104853504 G:\           GratefulDead

PS N:\> Get-CimInstance -Class Win32_LogicalDisk

DeviceID DriveType ProviderName   VolumeName         Size          FreeSpace
-------- --------- ------------   ----------         ----          ---------
A:       4
C:       3                        Windows            988877418496  771926069248
D:       3                        Big!              1990045179904  689684144128
E:       5
G:       4         \Music\GratefulDead              988877418496  771926069248


PS C:\> Get-CimInstance -Class Win32_NetworkConnection
LocalName RemoteName            ConnectionState Status
--------- ----------            --------------- ------
G:        \Music\GratefulDead  Connected       OK

此示例将 Get-PSDrive 显示的文件系统驱动器类型与使用其他方法显示的文件系统驱动器类型进行比较。此示例演示了在 Windows PowerShell 中显示驱动器的不同方法,并表明使用 New-PSDrive cmdlet 创建的会话特定驱动器只能在 Windows PowerShell 中访问。

第一个命令使用 Get-PSDrive 获取会话中的所有文件系统驱动器。这包括固定驱动器(C: 和 D:)、使用 New-PSDrivePersist 参数创建的映射网络驱动器 (G:) 以及使用 New-PSDrive 创建的 PowerShell 驱动器 (T:),不带 Persist 参数。

net use 命令显示 Windows 映射的网络驱动器,在本例中它仅显示 G 驱动器。它不显示由 New-PSDrive 创建的 X: 驱动器。它表明 G: 驱动器也映射到 \\Music\GratefulDead。

第三个命令使用 Microsoft .NET Framework System.IO.DriveInfo 类的 GetDrives 方法。此命令获取 Windows 文件系统驱动器,包括驱动器 G:,但它不会获取由 New-PSDrive 创建的驱动器。

第四个命令使用 Get-CimInstance cmdlet 获取 Win32_LogicalDisk 类的实例。它返回 A:、C:、D:、E: 和 G: 驱动器,但不返回由 New-PSDrive 创建的驱动器。

最后一个命令使用 Get-CimInstance cmdlet 显示 Win32_NetworkConnection 类的实例。与net use一样,它仅返回由New-PSDrive创建的持久G:驱动器。

参数

-LiteralName

指定驱动器的名称。

LiteralName 的值完全按照键入的方式使用。没有字符被解释为通配符。如果名称包含转义字符,请将其括在单引号中。单引号告诉 Windows PowerShell 不要将任何字符解释为转义序列。

类型 :

String[]

位置:

0

默认值:

None

必需的:

True

接受管道输入:

True

接受通配符:

False

-Name

以字符串数组形式指定此 cmdlet 在操作中获取的驱动器名称或名称。键入不带冒号的驱动器名称或字母 (:)。

类型 :

String[]

位置:

0

默认值:

None

必需的:

False

接受管道输入:

True

接受通配符:

False

-PSProvider

以字符串数组形式指定 Windows PowerShell 提供程序。此 cmdlet 仅获取此提供程序支持的驱动器。键入提供程序的名称,例如文件系统、注册表或证书。

类型 :

String[]

位置:

命名

默认值:

None

必需的:

False

接受管道输入:

True

接受通配符:

False

-Scope

指定此 cmdlet 获取驱动器的范围。

该参数可接受的值为:

  • 全球的
  • 当地的
  • 脚本
  • 相对于当前作用域的数字(0 到作用域数量,其中 0 是当前作用域,1 是其父作用域)。 “本地”是默认值。

有关详细信息,请参阅 about_Scopes。

类型 :

String

位置:

命名

默认值:

None

必需的:

False

接受管道输入:

True

接受通配符:

False

输入

您无法将对象通过管道传输到此 cmdlet。

输出

PSDriveInfo

此 cmdlet 返回表示会话中驱动器的对象。

笔记

PowerShell 包含 Get-PSDrive 的以下别名:

  • 所有平台:

      gdr
  • 此 cmdlet 旨在处理任何提供程序公开的数据。要列出会话中可用的提供程序,请使用 Get-PSProvider cmdlet。有关详细信息,请参阅 about_Providers。

  • 使用 New-PSDrive cmdlet 的 Persist 参数创建的映射网络驱动器特定于用户帐户。您在使用“以管理员身份运行”选项或使用其他用户的凭据启动的会话中创建的映射网络驱动器在没有显式凭据或使用当前用户的凭据启动的会话中不可见。

  • 您需要 登录账户 后才能发表评论

    取消回复欢迎 发表评论:

    关灯