2017-07-18 40 views
0

我試圖使用PowerShell下面PowerShell來查找文件夾/文件大小

$StartFolder = "D:\" 
$Output = "C:\Temp\test-d.csv" 

Add-Content -Value "Folder Path|Size" -Path $Output 

$colItems = (Get-ChildItem $startFolder -Recurse | Measure-Object -Property Length -Sum) 
"$StartFolder -- " + "{0:N2}" -f ($colItems.Sum/1MB) + " MB" # | Out-File $Output 

$colItems = (Get-ChildItem $startFolder -Recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object) 
foreach ($i in $colItems) { 
    $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object -Property Length -Sum) 
    $i.FullName + "|" + "{0:N2}" -f ($subFolderItems.Sum/1MB) + " MB" | Out-File $Output -Append 
} 

我收到錯誤提到獲得的文件夾/文件大小寫一個腳本如下所述:

 
Measure-Object : The property "Length" cannot be found in the input for any 
objects. 
At line:12 char:65 
+   $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object - ... 
+ 
    + CategoryInfo   : InvalidArgument: (:) [Measure-Object], PSArgumentException 
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand 

Measure-Object : The property "Length" cannot be found in the input for any 
objects. 
At line:12 char:65 
+   $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object - ... 
+ 
    + CategoryInfo   : InvalidArgument: (:) [Measure-Object], PSArgumentException 
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand 

此外,當我的目標C:盤我收到「拒絕訪問」上的一些系統文件:

 
Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup' 
is denied. 
At line:12 char:28 
+   $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object - ... 
+        
    + CategoryInfo   : PermissionDenied: (C:\Windows\Syst...es\WMI\RtBackup:String) [Get-ChildItem], UnauthorizedAccessException 
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand 
+0

如果您不是管理員或沒有權限,您將無法修改系統目錄。因此,您可以嘗試以管理員身份運行或提供必要的權限。 由於無法訪問,因此無法挑選長度。 –

+0

爲什麼你會用這樣的管道連接table viewl? PowerShell內置了對csv,json的支持,並且還有用於excel的模塊... – Clijsters

回答

2

DirectoryInfo對象沒有屬性Length,因此您需要將大小計算限制爲文件。

$colItems = Get-ChildItem $startFolder -Recurse -Force | 
      Where-Object { -not $_.PSIsContainer } | 
      Measure-Object -Property Length -Sum 

可能更容易使用Scripting.FileSystemObject COM對象,因爲這將讓你得到目錄對象使用其內容的大小。您可能想要使用Export-Csv將您的數據導出爲CSV。使用calculated properties來構建所需的對象。

$fso = New-Object -COM 'Scripting.FileSystemObject' 

$folders = @($StartFolder) 
$folders += Get-ChildItem $StartFolder -Recurse -Force | 
      Where-Object { $_.PSIsContainer } | 
      Select-Object -Expand FullName 

$folders | 
    Select-Object @{n='Path';e={$_}}, @{n='Size';e={$fso.GetFolder($_).Size}} | 
    Export-Csv $Output -Delimiter '|' -NoType 
+1

'select-object'中的輕微語法錯誤。應該是一個逗號,而不是一個分號分隔的兩個項目:'選擇-對象@ {N = '路徑'; E = {$ _}},@ {N = '尺寸'; E = {$ fso.GetFolder($ _)。Size}} |' – Itchydon

+1

確實。感謝您的高舉。固定。 –

+0

@ Ansgar Wiechers&Itchydon,謝謝您的回覆,並對延遲迴復表示歉意。我也尋找到讓隱藏文件的大小,以及與其他文件大小一起......有沒有一種方法,我們可以做的,你可以請讓我知道... –