This topic contains 11 replies, has 5 voices, and was last updated by random commandline 2 years, 3 months ago.
-
AuthorPosts
-
January 19, 2016 at 12:09 pm #34072
Hi guys, I need to create a report in Excel format for my boss.
The boss wan'tThis is the script I have modified that I found online.
$rootPath = "E:\"
$colItems1 = Get-ChildItem $rootPath
foreach ($i in $colItems1)
{
$colItems = (Get-ChildItem -recurse "$rootPath\$i" | Measure-Object -property length -sum)
"{0:N2}" -f ($colItems.sum / 1000MB) + " GB,$i"
"{0:N2}" -f ($colItems.sum / 1000MB) + " GB,$i" >>C:\result.txt
}The script are great, and gives me the username / name of the folder and the size of the folder.
But it is not sorting the numbers from biggest to smallest.
If i use the descending parameter, It is sorting the name of the folder from A to B and not the numbers. As I want to.Please help me guys.
-
January 19, 2016 at 1:47 pm #34075
I would probably approach it a little differently than the original code – here's my take:
$report = @() $rootPath = 'C:\Tools' $colItems1 = Get-ChildItem -Path $rootPath -Directory foreach ( $item in $colItems1 ) { $folderSize = Get-ChildItem -Path $item.FullName -Recurse | Measure-Object -Property Length -Sum $folderInfo = New-Object -TypeName PSObject -Property @{ 'Folder'=$item.FullName; 'FolderSize'=$folderSize.Sum; } $report += $folderInfo } $report
You could take the content in $report and fork the output to file and screen using something like $report | tee-object 'report.txt'. You could also convert the folder size to GB or MB or whatever when you look at the output in $report by dividing it by whatever unit of measure you prefer.
-
January 19, 2016 at 1:48 pm #34076
Oh, and obviously, you can sort the output
$report | Sort-Object -Property FolderSize -Descending
-
January 19, 2016 at 1:49 pm #34077
You need to specify a property, in this case Length, to sort. This will sort properly and export to csv file. In your script, it looks like you are writing to disk for each item and using Get-Childitem multiple times. Multiple writes and other things can cause performance issues. I recommend minimizing your disk writes.
$allitems = $null; $allitems = {@()}.Invoke() # Put item name and length in object then add to collection foreach ($item in $colitems){ $itemprop = [PSCustomObject]@{ Name = $item.Name Length = "{0:N2} (GB)" -f ($item.Length / 1GB) } $allitems.Add($itemprop) } # Make one write to disk $allitems | Sort-Object -Property Length -Descending | Export-Csv .\result.csv -NoTypeInformation -Append -NoClobber
-
January 19, 2016 at 2:08 pm #34078
I took the same approach as the other Matt.
$rootPath = "F:\" $folders = Get-ChildItem $rootPath -Directory $objCol = @() foreach ($folder in $folders) { $size = Get-ChildItem $folder -Recurse | Measure-Object -Property Length -sum $name = $folder.Name $obj = [PSCustomObject] @{ FolderName = $name FolderSize = "{0:N5}" -f ($size.sum/1GB) } $objCol += $obj } $objCol | Sort-Object FolderSize -Descending | Export-Csv F:\report.csv -NoTypeInformation
-
January 20, 2016 at 11:58 pm #34115
Hi again guys, I have tried all of the codes you posted, but none of them worked.
The only code that Works, Is the code I posted, but it is not sorting it from big to small numbers. -
January 21, 2016 at 9:06 am #34129
I made a slight modification to your original script that should work for you. Sorting before using the format operator is the key.
$rootPath = "E:\" $colItems1 = Get-ChildItem $rootPath $allitems = $null; $allitems = {@()}.Invoke() foreach ($i in $colItems1) {$colItems = (Get-ChildItem -recurse "$rootPath\$i" | Measure-Object -property length -sum) $itemprop = [PSCustomObject]@{Name = $i.Name; Length = $colItems.sum} $allitems.Add($itemprop)} # Export contents $allitems | Sort-Object -Property Length -Descending | Select-Object @{name='Name';exp = {$_.name}}, @{name='Length';exp={"{0:N2} (GB)" -f ($_.Length /1GB)}} | Export-Csv .\result.csv -NoTypeInformation -Append -NoClobber
-
January 22, 2016 at 12:18 am #34168
Thank you,
But again it Is still not working, this is the output error Messages I geet.
Really don't know what it is.
I'm using Powershell 3.0 Maybe this has something to do With it.
Would It matter if I use PS Session to do it? On my win 10 Client I have PowerShell 5.0PS C:\> $rootPath = "E:\"
$colItems1 = Get-ChildItem $rootPath
$allitems = $null; $allitems = {@()}.Invoke()
foreach ($i in $colItems1)
{$colItems = (Get-ChildItem -recurse "$rootPath\$i" | Measure-Object -property length -sum)
$itemprop = [PSCustomObject]@{Name = $i.Name; Length = $colItems.sum}
$allitems.Add($itemprop)}# Export contents
$allitems | Sort-Object -Property Length -Descending |
Select-Object @{name='Name';exp = {$_.name}},
@{name='Length';exp={"{0:N2} (GB)" -f ($_.Length /1GB)}} |
Export-Csv C:\resultcontact.csv -NoTypeInformation -Append -NoClobber
Measure-Object : Property "length" cannot be found in any object(s) input.
At line:5 char:55
+ {$colItems = (Get-ChildItem -recurse "$rootPath\$i" | Measure-Object -property l ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
+ FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand
Measure-Object : Property "length" cannot be found in any object(s) input.
At line:5 char:55
+ {$colItems = (Get-ChildItem -recurse "$rootPath\$i" | Measure-Object -property l ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException
+ FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand -
January 22, 2016 at 2:01 am #34171
Hi again, I talked to early.
It actually worked.
You are the best 🙂 -
January 22, 2016 at 3:02 am #34174
This is really great Random Commandline, do you have some tips to me.
How can I learn PowerShell like you, do you have any background I'm impressed.Recommend me books, Videos and all the Things you can recommend.
I also have a Subscription With pluralsight.
Also I am Norwegian, so my English is a little bit out of shape. -
January 22, 2016 at 5:15 am #34176
Outstanding Books and Videos are:
Books: by Don Jones and Jeffery Hicks
http://www.amazon.com/Learn-Windows-PowerShell-Month-Lunches/dp/1617291080Videos:
https://mva.microsoft.com/en-us/training-courses/getting-started-with-powershell-30-jump-start-8276 -
January 22, 2016 at 6:03 am #34181
Excellent, glad I could help. PluralSight has great content. My title is System Engineer, but I consider myself more of a System Administrator. My background consists of Windows/Linux Server, VMware, Active Directory, Exchange, SQL, thin client deployment. I have used or try to use PowerShell in all of these areas when it is useful. David recommended some great books, but before diving into toolmaking I recommend starting with the books below.
PowerShell TFM 4th Edition
(A great overview of what you can do with PowerShell – Powershell install, file/folder manipulation, storing, exporting data, intro in Active Directory, etc )
PowerShell in Action 2nd Edition
(One of my current favorites – This goes into granular detail on why things work and being more efficient in PowerShell — operators, flow control, remoting, error handling, etc) -
AuthorPosts
You must be logged in to reply to this topic.