2017-08-10 71 views
1

我使用這個命令來獲得我的驅動器列表我WindowsPowerShell的獲得驅動器列表

get-wmiobject win32_volume | ? { $_.DriveType -eq 3 } | % { get-psdrive $_.DriveLetter[0] } 

這給:

Name   Used (GB)  Free (GB) Provider  Root 
----   ---------  --------- --------  ---- 
C     131.85  333.62 FileSystem C:\ 
D     111.15  200.63 FileSystem D:\ 

我真正想要得到的是「根」列下的值列表。 基本上一個字符串如下C:\ D:\

我該怎麼做?

編輯

我成功地做到這一點:

get-wmiobject win32_volume | ? { $_.DriveType -eq 3 } | % { get-psdrive $_.DriveLetter[0] } | Select Root 

這給:

Root 
    ---- 
    C:\ 
    D:\ 

如何將其轉換爲:

C:\ D:\ 
+0

@LotPings:你在哪裏撿只有root。他希望只選擇根目錄 –

+0

使用:'Select -Expand Root'但是您的查詢存在缺陷,具體取決於您可能獲得的驅動器(不可用)'無法在'get-psdrive $ __中編入空數組' .DriveLetter [0]' – iRon

+0

@iRon你會如何解決它? – avi

回答

1

試試這個:

(get-wmiobject win32_volume | ? { $_.DriveType -eq 3 } | % { get-psdrive $_.DriveLetter[0] }).Root 

它會顯示一行一行:

C:\ 
D:\ 

否則你可以像這樣把它並排:

(get-wmiobject win32_volume | ? { $_.DriveType -eq 3 } | % { get-psdrive $_.DriveLetter[0] }).Root -join " " 

它將輸出像這個:

C:\ D:\ 

希望它hel PS。

0

爲了防止類似的錯誤:

Cannot index into a null array. 
At line:1 char:82 
+ ... 3 } | % {$_.DriveLetter} | % { get-psdrive $_.DriveLetter[0] } | Sel ... 
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : NullArray 

我將包括Where子句中-and $_.DriveLetter

而且我認爲沒有必要使用Get-PSDrive,因爲所需的輸出已經在Name中可用。

這樣:

get-wmiobject win32_volume | ? {$_.DriveType -eq 3 -and $_.DriveLetter} | Select -Expand Name 
相關問題