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\一個BarerDead。
第三個命令使用 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 取得磁碟驅動器的範圍。
此參數可接受的值為:
- 全球
- 本機
- 指令碼
- 相對於目前範圍的數位, (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 參數來建立的連線網路磁碟機是使用者帳戶特定的磁碟機。 您在以 [以系統管理員身分執行] 選項啟動的會話中建立的對應網路驅動器機,或在未明確認證或目前使用者的認證啟動的會話中,看不到其他使用者的認證。