I am looking for a solution to this problem: Find the summary size of .jpg and .bmp files in Windows and its subdirectories. I use the following commands:
Get-ChildItem -Force -Depth 2 -Path C:\Windows\ | Where-Object -Property Extension -Match "\.(bmp|jpg)$" | Measure-Object -Property Length -Sum | Select-Object -Property Sum
How do I count the actual summary size without getting an access error: The output is:
Get-ChildItem : Access to the path 'C:\Windows\CSC\v2.0.6' is denied.
At line:1 char:1
Get-ChildItem -Force -Depth 2 -Path C:\Windows\ | Where-Object -Prope ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : PermissionDenied: (C:\Windows\CSC\v2.0.6:String) [Get-ChildItem], UnauthorizedAccessException
FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
Sum
---
35241685
Thank You.
CodePudding user response:
To get all *.bmp and *.jpg files and to sum their length you can do:
$measure = Get-ChildItem -path $env:SystemRoot* -Include @('*.bmp','*.jpg') -ErrorAction:SilentlyContinue -Recurse | Measure-Object -Property length -sum
But this is quite slow. By using the parameter "-Filter" this runs much much faster, but you have to do 2 calls as the parameter "-Filter" only accepts a string and not a array:
#Get files
$files = @(
get-childitem -path $env:SystemRoot -Filter '*.jpg' -Recurse -ErrorAction:SilentlyContinue
get-childitem -path $env:SystemRoot -Filter '*.bmp' -Recurse -ErrorAction:SilentlyContinue
)
#Get count and sum
$measure = $files | Measure-Object -Property length -sum
Finally to avoid the "path too long" issue:
#Get files
$files = @(
get-childitem -pspath "\\?\C:\Windows" -Filter '*.jpg' -Recurse -ErrorAction:SilentlyContinue
get-childitem -pspath "\\?\C:\Windows" -Filter '*.bmp' -Recurse -ErrorAction:SilentlyContinue
)
#Get count and sum
$measure = $files | Measure-Object -Property length -sum
See: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry