Get-PSDrive

在當前場次獲得硬碟。

語法

Name (預設值)

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

LiteralName

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

Description

Get-PSDrive指令檔在當前會話中取得硬碟。 您可以在工作階段中選擇取得特定的磁碟驅動器或所有的磁碟驅動器。

此指令包可獲得以下類型的硬碟:

  • 電腦上的 Windows 邏輯磁碟,包括映射到網路共享的磁碟。
  • 由 PowerShell 提供者暴露的磁碟機(例如 Certificate:、Function:、Alias: 磁碟機)以及 Windows PowerShell 登錄檔提供者所暴露的 HKLM: 和 HKCU: 磁碟機。
  • 會話指定的暫存硬碟和你用 New-PSDrive 指令碼建立的持久映射網路硬碟。

從 PowerShell 3.0 Windows開始,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:)所暴露的硬碟。

範例二:在電腦上安裝一個硬碟

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 指令碼建立的暫存磁碟。

範例 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 指令碼建立的會話專用磁碟只能在 PowerShell 中存取Windows。

第一個指令用 Get-PSDrive 來取得會話中所有的檔案系統磁碟機。 這包括固定磁碟(C: 和 D:)、一個透過 Persist 參數 New-PSDrive建立的映射網路磁碟機(G:),以及一個使用 New-PSDrivePersist 參數建立的 PowerShell 磁碟機(T:)。

net 使用指令顯示Windows映射的網路磁碟,此時僅顯示 G 磁碟。 它不會顯示由 New-PSDrive. 它顯示 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 類別的實例。 與 網路使用類似,它只會回傳由 New-PSDrive所建立的持久 G: 磁碟機。

參數

-LiteralName

指定硬碟名稱。

LiteralName 的值會完全按照其型別方式使用。 不會將任何字元解譯為通配符。 若名稱包含轉義字元,請以單引號包圍。 單引號告訴 Windows PowerShell 不要將任何字元解讀為逃脫序列。

參數屬性

類型:

String[]

預設值:None
支援萬用字元:False
不要顯示:False

參數集

LiteralName
Position:0
必要:True
來自管線的值:False
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

-Name

以字串陣列的形式指定該指令小程式在操作中獲得的磁碟名稱或名稱。 請輸入磁碟機名稱或字母,不加冒號(:)。

參數屬性

類型:

String[]

預設值:None
支援萬用字元:False
不要顯示:False

參數集

Name
Position:0
必要:False
來自管線的值:False
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

-PSProvider

指定一個字串陣列,指定 Windows PowerShell 提供者。 此指令只接收該供應商支援的硬碟。 輸入提供者名稱,例如檔案系統、登錄檔或憑證。

參數屬性

類型:

String[]

預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

-Scope

指定此指令子取得硬碟的範圍。

此參數可接受的值為:

  • 全球
  • 本地
  • Script
  • 相對於目前範圍的數字(0 除以範圍數,0 是當前範圍,1 是其父範圍)。 「本地」是預設。

如需詳細資訊,請參閱 about_Scopes。

參數屬性

類型:String
預設值:None
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

CommonParameters

此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。

輸入

None

您不能將物件透過管道傳送到此 Cmdlet。

輸出

PSDriveInfo

此指令檔回傳代表會話中磁碟的物件。

備註

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

  • 所有平臺:

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

  • 使用 New-PSDrive 指令檔的 Persist 參數建立的映射網路磁碟機,是專屬於使用者帳號的。 你在以「以管理員身份執行」選項或使用其他使用者憑證啟動的會話中建立的映射網路磁碟,在沒有明確憑證或使用當前使用者憑證啟動的會話中是看不到的。