Get-PSDrive
获取当前会话中的驱动器。
语法
Get-PSDrive
[[-Name] <String[]>]
[-Scope <String>]
[-PSProvider <String[]>]
[<CommonParameters>]
Get-PSDrive
[-LiteralName] <String[]>
[-Scope <String>]
[-PSProvider <String[]>]
[<CommonParameters>]
说明
cmdlet Get-PSDrive
获取当前会话中的驱动器。 可以获取会话中的特定驱动器或所有驱动器。
此 cmdlet 获取以下类型的驱动器:
- 计算机上的 Windows 逻辑驱动器,包括映射到网络共享的驱动器。
- 由 PowerShell 提供程序公开的驱动器 (,例如证书:、函数:和别名:驱动器) ,以及Windows PowerShell注册表提供程序公开的 HKLM: 和 HKCU: 驱动器。
- 通过使用 cmdlet 创建的会话指定的临时驱动器和永久的映射网络驱动器。
从 Windows PowerShell 3.0 开始,cmdlet 的 New-PSDrive
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提供程序公开的驱动器 (别名:、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 FileSystem 提供程序支持的所有驱动器。 这包括固定驱动器、逻辑分区、映射网络驱动器和使用 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:) 、使用 的 Persist 参数 New-PSDrive
创建的映射网络驱动器 (G:) ,以及通过使用 New-PSDrive
不使用 Persist 参数创建的 T:) PowerShell (驱动器。
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 不要将所有字符都解释为转义序列。
Type: | String[] |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Name
指定此 cmdlet 在操作中获取的驱动器的名称(作为字符串数组)。 键入驱动器名称或驱动器号,不带冒号 (:)。
Type: | String[] |
Position: | 0 |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-PSProvider
以字符串数组的形式指定Windows PowerShell提供程序。 此 cmdlet 仅获取此提供程序支持的驱动器。 键入提供程序的名称,如 FileSystem、Registry 或 Certificate。
Type: | String[] |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Scope
指定此 cmdlet 获取驱动器的范围。
此参数的可接受值为:
- 全球
- Local
- 脚本
- 相对于当前范围的数字 (0 到范围数,其中 0 是当前范围,1 是其父) 。 默认值为“Local”。
有关详细信息,请参阅 about_Scopes。
Type: | String |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
输入
None
不能通过管道将对象传递给此 cmdlet。
输出
此 cmdlet 返回表示会话中的驱动器的对象。
备注
- 此 cmdlet 用于处理由任何提供程序公开的数据。 若要列出会话中的可用提供程序,请使用
Get-PSProvider
cmdlet。 有关详细信息,请参阅 about_Providers。 - 使用 New-PSDrive cmdlet 的 Persist 参数创建的映射网络驱动器是特定于用户帐户的。 在以管理员身份运行选项启动的会话中或使用其他用户凭据启动的会话中创建的映射网络驱动器在不使用显式凭据或当前用户的凭据启动的会话中不可见。