size of file

Kelly Garcia
Kelly Garcia used Ask the Experts™
on
Hi All,

I have the code below to find files that are older than 6 moths and greater than 1 gb:

$pathname = Read-Host "Please Enter Path Name: "
$files = Get-ChildItem $pathname -Recurse -File | 
    Where-Object { $_.LastAccessTime -le (Get-date).AddMonths(-6) -and $_.Length -ge 1GB } | 
  Select FullName,CreationTime,LastAccessTime,LastWriteTime |
Export-Csv "c:\$(($pathname -split "\\")[-1]).csv" -NTI 

Open in new window


I need to add to the spreadsheet the size of the files, how do I do this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PowerShell Developer
Top Expert 2010
Commented:
Length is the size of the file (in bytes). This example writes the file size in MB to the CSV file.
$pathname = Read-Host "Please Enter Path Name: "
$files = Get-ChildItem $pathname -Recurse -File | 
    Where-Object { $_.LastAccessTime -le (Get-date).AddMonths(-6) -and $_.Length -ge 1GB } | 
  Select FullName,CreationTime,LastAccessTime,LastWriteTime, @{n='Size';e={ [Math]::Round($_.Length / 1MB, 2) }} |
Export-Csv "c:\$(($pathname -split "\\")[-1]).csv" -NTI 

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial