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.
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.
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
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.
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.0
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 …
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.
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)