Link to home
Start Free TrialLog in
Avatar of grayjfiii
grayjfiii

asked on

Directory Size and File count

I have the following script to locate a file called "user.db". How can I get a file count and directory size when the file is found.

$Computer = $env:computername
$strName = $env:username
$Filname = $Computer + "_" + $strName + ".txt"
Get-ChildItem -Path c:\ -Filter "user.db" -Recurse -ErrorAction SilentlyContinue
foreach-object { $Computer + "," + $Computer +"," +  $_.Fullname + "," + $_.Length + "," +$_.LastAccessTime} |Out-File c:\Results\$Filname –width 1024
Avatar of BT15
BT15
Flag of United States of America image

I added the directory name, count of files in the directory, and directory size

$Computer = $env:computername
$strName = $env:username
$Filname = $Computer + "_" + $strName + ".txt"
Get-ChildItem -Path c:\ -Filter "user.db" -Recurse -ErrorAction SilentlyContinue | foreach-object { 
$dirsize = 0
$files = Get-ChildItem $_.directory -Recurse
foreach ($file in $files) {
	$dirsize += $file.length
}
	 
$Computer + "," + $Computer +"," +  $_.Fullname + "," + $_.Length + "," + $_.directory + "," + $files.count + "," + $dirsize + "," +$_.LastAccessTime }  |Out-File h:\$Filname –width 1024 

Open in new window

Avatar of grayjfiii
grayjfiii

ASKER

How can I add this to it to get MB for file size

$colItems = (Get-ChildItem C:\Scripts | Measure-Object -property length -sum)
"{0:N2}" -f ($colItems.sum / 1MB) + " MB"
ASKER CERTIFIED SOLUTION
Avatar of BT15
BT15
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes MB and GB if possible
Thank you for the fast response!