Powershell & Windows Batch & Vbscript: disk space monitoring & list various files with size and location

Luis Diaz
Luis Diaz used Ask the Experts™
on
Hello experts,

I am having some space disk issues and I would like to monitor this without any installation.

My idea is to launch a command: powershell & windows batch or vbscript in order to:

1-Drill down root folder: C:\
2-List the various files sorted by sizes (highest to lowest) in a csv or txt file with specific locations

This will allows me to check the various files which takes the most and remove them to free up disk space.

If you have questions, please contact me.

Regards,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Very simple in PowerShell, including a check to only log files over a given size, for example 50MB:
Get-ChildItem -Path C:\ -Recurse -Force -File | Where-Object {$_.Length -gt 50MB} | Select-Object Name, FullName, Length | Export-Csv -NoTypeInformation -Path C:\Temp\BigFiles.csv

Open in new window

Luis DiazIT consultant

Author

Commented:
Thank you oBdA, I will test it soon.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
I know you said "monitoring" in the post title, but based on the actual question description it felt more like analysis.  For what it's worth, you should look at TreeSize Free also, it comes in a portable so nothing to install, is small and fast, and does a good job of helping drill down into where the space hogs are.  Even if you use it in conjunction with some scripted monitoring...



»bp
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Luis DiazIT consultant

Author

Commented:
Thank you very  much for this proposal Bill. Do you know if portable versions can also be launched in Server?
When I reported monitoring I meant just to found the files which take the most space.
Luis DiazIT consultant

Author

Commented:
oBda three remarks.
1-Is it possible convert the size in Mb or kb?
2-I got some PermissionDenied for files which are located AppData. Possible to omit them? Is there a parameter to report for this?
2019-02-19_20h15_54.png3-For csv export with ";" delimiter what should I report in the command?
Thank you in advance for your help.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Try this:
Get-ChildItem -Path C:\ -Recurse -Force -File -ErrorAction SilentlyContinue |
	Where-Object {$_.Length -gt 50MB} |
	Select-Object Name, FullName, @{n='Length (MB)'; e={[math]::Round(($_.Length / 1MB), 2)}} |
	Export-Csv -NoTypeInformation -Delimiter ';' -Path C:\Temp\BigFiles.csv

Open in new window

Luis DiazIT consultant

Author

Commented:
Thank you. I will test it soon.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Yes, it should work on Server.


»bp
Luis DiazIT consultant

Author

Commented:
@oBda: the last proposal works fine just a final requirement which can be very helpful:
Sort file length from Ascending to descending.
Do you think this is possible with powershell?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Of course that's possible; just be aware that, depending on the amount of files found, significantly more memory might be used, because all the results will have to be stored before they can be sorted.
Without sorting, the elements will just be passed through and directly written to the export file, which requires next to no memory.
Get-ChildItem -Path C:\ -Recurse -Force -File -ErrorAction SilentlyContinue |
	Where-Object {$_.Length -gt 50MB} |
	Select-Object Name, FullName, @{n='Length (MB)'; e={[math]::Round(($_.Length / 1MB), 2)}} |
	Sort-Object -Property 'Length (MB)' |
	Export-Csv -NoTypeInformation -Delimiter ';' -Path C:\Temp\BigFiles.csv 

Open in new window

Luis DiazIT consultant

Author

Commented:
Thank  you for your comment oBdA in that case the best is to avoid sorting and then sorting directly with Excel.

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