The 25 largest files with Powershell

All we have encountered more than once out of space in our hard disks, usually at the most inopportune moment, and we had to dedicate ourselves to delete something to continue working.

Here I present a small script in Powershell which one we will find your largest files on your disk. Accepts two parameters:

  • The path where we want to check the size of the files. It can be an entire drive such as c:\ or, which is often more effective, a specific path such as the path where we keep our documents. The script searches recursively in the specified folder and its subfolders.
  • The second parameter is the number of files to display, starting with the largest. Normally between 25 or 50 largest usually left over something.

To use the script copies the following code (without the line numbers):

$Ruta = Read-Host 'Please, enter the route'
$NumFicheros = Read-Host 'Number of files to return'

Get-ChildItem -Path $Ruta -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -Recurse -Force -File | `
 Select-Object @{Name='Ruta';Expression={($_.FullName)}},@{Name='Tamaño';Expression={($_.Length/1MB)}} | `
 Sort-Object -Property Tamaño -Descending | `
 Select-Object -First $NumFicheros | Format-table Ruta, {$_.Tamaño.ToString("000000.00")} -HideTableHeaders 
pause

Inside an empty text file and name it 25Ficheros.ps1

The important thing is to have the extension ps1. We have to have Powershell installed on our system. If we have Windows 10 installed, we have it already. If not, install it from following this link.

To execute it, you must click with the right mouse button on the file created with the script and select the option Run with Powershell.

20160420_25Ficheros_Paso01

If this is the first time you execute a Powershell script, it will tell you if you want to change the execution policy, as by default the scripts executions are not allowed. You will indicate yes, and you are requested for the two execution parameters.

20160420_25Ficheros_Paso02

Write both data and press ENTER after each item. The script will begin its work and after a moment (the more generic route to search in, the longer it takes) you will obtain the results in two columns. To the left, the file name with full path, and on the right the megas occupying the file.

20160420_25Ficheros_Paso03

For example, for my directory C:\Windows\System32 (and all subfolders), these are my top 25 files:

20160420_25Ficheros_Paso04

Press Enter again to close the window.

I hope you find it useful the script. For any comments do not hesitate to contact via links on social networks or email.

 

Isaac Lázaro

gigaSACs is a website dedicated to writing about Cloud Computing and programming, the main subjects are Azure, .NET, SQL Server, Wintel, Linux, JAVA, and general technology.