Get-PSDrive

取得目前工作階段中的磁碟驅動器。

Syntax

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

Description

Cmdlet 會 Get-PSDrive 取得目前會話中的磁碟驅動器。 您可以在工作階段中取得特定磁碟驅動器或所有磁碟驅動器。

此 Cmdlet 會取得下列類型的磁碟驅動器:

  • 計算機上的 Windows 邏輯磁碟驅動器,包括對應至網路共用的磁碟驅動器。
  • PowerShell 提供者公開的磁碟驅動器(例如 Certificate:、Function:和 Alias: 磁碟驅動器)和 HKLM:和 HKCU:Windows PowerShell 登錄提供者所公開的磁碟驅動器。
  • 會話指定的暫存磁碟驅動器和您使用 New-PSDrive 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 提供者公開的磁碟驅動器(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 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 參數所建立的對應網路驅動器機 (G:),以及使用 New-PSDrive 不使用 Persist 參數New-PSDrive所建立的 PowerShell 磁碟驅動器 (T:)。

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_Network連線 ion 類別的實例。 如同 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 取得磁碟驅動器的範圍。

此參數可接受的值為:

  • 全球
  • 區域
  • 指令碼
  • 相對於目前範圍的數位(0 到範圍的數目,其中0是目前的範圍,1是其父系)。 “Local” 是預設值。

如需詳細資訊,請參閱 about_Scopes

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-UseTransaction

在作用中交易中包含 命令。 只有在交易進行中時,此參數才有效。 如需詳細資訊,請參閱 about_Transactions

Type:SwitchParameter
Aliases:usetx
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

輸入

None

您無法使用管線將物件傳送至此 Cmdlet。

輸出

PSDriveInfo

此 Cmdlet 會傳回代表會話中磁碟驅動器的物件。

備註

Windows PowerShell 包含下列的 Get-PSDrive別名:

  • gdr

  • 此 Cmdlet 的設計目的是要處理任何提供者所公開的數據。 若要列出會話中可用的提供者,請使用 Get-PSProvider Cmdlet。 如需詳細資訊,請參閱 about_Providers

  • 使用 New-PSDrive Cmdlet 的 Persist 參數所建立的對應網路驅動器機是用戶帳戶特有的。 您在會話中建立的對應網路驅動器機,這些磁碟驅動器是以 [以系統管理員身分執行] 選項或以其他使用者的認證啟動的會話中看不到的,這些會話是在沒有明確認證或目前使用者的認證的情況下啟動的。