I’ve got a number of directories to search that contain large files (backups) and small files (md5’s).
I’ve written a script that displays the files in a way that enables me to quickly validate the backups are current and there is an associated md5 for the backup.
As a further script refinement I’d like to modify the file size display so rather than displaying the number of bytes in the large files the size is displayed as xx Gb, xx Mb, xx Kb as appropriate. In other words the value displayed in the file size column would be formatted differently depending on the size of the file.
The following works to display file size in bytes, Kb, Mb, or Gb, depending which is selected, but is applied to all file sizes. I have been unable to create conditional logic to format the display dependent on file size.
@{E={'{0,7:N0} bytes' -f ($_.Length)}}
@{E={'{0,7:N0} Kb ' -f ($_.Length/1kb)}}
@{E={'{0,7:N0} Mb ' -f ($_.Length)/1mb}}
@{E={'{0,7:N0} Gb ' -f ($_.Length/1gb)}}
The current script is as follows. I believe I need to modify a Property of the Select-Object but have been unable to figure out how to make a conditional format property. Any help would be greatly appreciated.
# Call this script from directory where backup files are being checked for current dates.
# The script takes the 20 most recent spi and md5 files in the directory, groups them
# by drive and sorts in descending order by increment number and age
#
Get-ChildItem | `
Where-Object {$_.name -like "*spi" -or $_.name -like "*md5"} | `
Sort-Object LastWriteTime -Descending | `
Select-Object -First 20 `
-Property @{L='Drv';E={($_.Name.substring(0,1))}}, Name, LastWriteTime, @{L='Size';E={ '{0,14:N0}' -f ($_.Length)}} | `
Sort-Object @{E='Drv'}, @{E={($_.Name.substring(12,$_.Name.Indexof(".")-12))};Descending=$true}, @{E='LastWriteTime';Descending=$true} | `
ft -AutoSize -GroupBy Drv
Sample output from the above script is below.
PS G:\Backup2\Cust1\Dev1> G:\inspect_dir.ps1 Drv: C Drv Name LastWriteTime Size --- ---- ------------- ---- C C_VOL-b003-i9107-cd.md5 5/19/2015 12:02:22 AM 59 C C_VOL-b003-i9107-cd.spi 5/18/2015 11:32:13 PM 99,440,640 C C_VOL-b003-i9060-cd.md5 5/18/2015 12:04:49 AM 59 C C_VOL-b003-i9060-cd.spi 5/17/2015 11:31:08 PM 98,367,488 C C_VOL-b003-i9013-cd-cw.md5 5/18/2015 12:04:10 AM 62 C C_VOL-b003-i9013-cd-cw.spi 5/16/2015 11:31:59 PM 125,943,296 C C_VOL-b003-i9013-cd.md5 5/18/2015 12:03:14 AM 59 C C_VOL-b003-i9013-cd.spi 5/16/2015 11:31:59 PM 98,432,000 Drv: D Drv Name LastWriteTime Size --- ---- ------------- ---- D D_VOL-b003-i9104-cd.md5 5/19/2015 12:03:02 AM 59 D D_VOL-b003-i9104-cd.spi 5/18/2015 11:31:18 PM 5,874,688 D D_VOL-b003-i9057-cd.md5 5/18/2015 12:06:31 AM 59 D D_VOL-b003-i9057-cd.spi 5/17/2015 11:31:04 PM 158,806,528 D D_VOL-b003-i9010-cd-cw.md5 5/18/2015 12:05:56 AM 62 D D_VOL-b003-i9010-cd.md5 5/18/2015 12:05:24 AM 59 Drv: E Drv Name LastWriteTime Size --- ---- ------------- ---- E E_VOL-b003-i9109-cd.md5 5/19/2015 12:02:41 AM 59 E E_VOL-b003-i9109-cd.spi 5/18/2015 11:30:34 PM 1,001,984 E E_VOL-b003-i9062-cd.md5 5/18/2015 12:05:11 AM 59 E E_VOL-b003-i9062-cd.spi 5/17/2015 11:30:31 PM 1,013,248 E E_VOL-b003-i9015-cd-cw.md5 5/18/2015 12:05:04 AM 62 E E_VOL-b003-i9015-cd.md5 5/18/2015 12:05:00 AM 59