Dec 03 2020 02:23 AM - edited Dec 03 2020 02:32 AM
My requirement is to retrieve all files from a directory when provided as an input to a certain folder level with the following properties: Name, FullName, Length, LastWriteTime.
Current code:
$Directory = 'Input'
Get-ChildItem -Path $Directory -Recurse -Depth 2 |
Select-Object Name, FullName, Length, LastWriteTime |
Where-Object {$_.Name -like "*.extension" -or $_.Name -like "*.extension" -or $_.Name -like "extension*"} |
Sort-Object -Property FullName |
Format-Table Name, FullName, Length, LastWriteTime
Current Output:
Name FullName Length LastWriteTime
----------- --------------------------- --------- --------------------
Vol001.zip C:\Users\ABC\Vol001.zip 100 11/09/2020 11:40:37
Vol002.zip C:\Users\ABC\Vol002.zip 100 11/09/2020 11:40:37
XY_Vol001 C:\Users\XY_Vol001\ 11/09/2020 11:40:47
XY_Vol002 C:\Users\XY_Vol002\ 11/09/2020 11:41:30
I would like the folder size to be provided instead of blank values in Length property field.
I have tried the below by adding
"@{ n = 'Length'; e = {("{0:N8}" -f ((Get-ChildItem -path 'Input' -recurse | Measure-Object -property length -sum ).sum /1MB) + " MB")}}"
like mentioned below:
$Directory = 'Input'
Get-ChildItem -Path $Directory -Recurse -Depth 2 |
Select-Object Name, FullName, Length, LastWriteTime,
@{ n = 'Length'; e = {("{0:N8}" -f ((Get-ChildItem -path 'Input' -recurse | Measure-Object -property length -sum ).sum /1MB) + " MB")}} |
Where-Object {$_.Name -like "*.extension" -or $_.Name -like "*.extension" -or $_.Name -like "extension*"} |
Format-Table Name, FullName, Length, LastWriteTime
But it is giving parent folder size for all the rows instead of giving their individual sizes.
My guess it would require looping.
Thanks!
Dec 16 2020 02:59 AM
@Abdullah_Shurjeel how about this:
$Directory = 'Input'
#Collect a list of all the files and folders
$FilesAndFolders =Get-ChildItem -Path $Directory -Recurse -Depth 2
#Loop through the files and folders
Foreach ($FileOrFolder in $FilesAndFolders) {
if ($FileOrFolder.Attributes -contains "Directory") {
#This is a folder - calculate the total folder size
$Length= (Get-ChildItem $FileOrFolder -Recurse | Measure-Object -Sum Length).Sum
$FileOrFolder | Select-Object Name, FullName, @{Name="Length"; Expression={$Length}}, LastWriteTime
} else {
#This is a file
$FileOrFolder | Select-Object Name, FullName, Length, LastWriteTime
}
}
Dec 17 2020 12:09 AM
I am getting the below error:
Measure-Object : The property "Length" cannot be found in the input for any objects.
At line:8 char:58
+ ... et-ChildItem $FileOrFolder -Recurse | Measure-Object -Sum Length).Sum
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
+ FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand
Dec 17 2020 01:23 AM
Solution@Abdullah_Shurjeel It looks like Measure-Object Windows PowerShell is behaving slightly differently to PSCore. I've changed the $Length= .... line to the following which appears to be working as expected.
$Length= (Get-ChildItem $FileOrFolder.FullName -Recurse | Where-Object {$_.Attributes -notcontains "Directory"} | Measure-Object -Sum -Property Length).Sum
Mar 03 2021 11:55 PM
Dec 17 2020 01:23 AM
Solution@Abdullah_Shurjeel It looks like Measure-Object Windows PowerShell is behaving slightly differently to PSCore. I've changed the $Length= .... line to the following which appears to be working as expected.
$Length= (Get-ChildItem $FileOrFolder.FullName -Recurse | Where-Object {$_.Attributes -notcontains "Directory"} | Measure-Object -Sum -Property Length).Sum