DEV Community

box
box

Posted on

Identify Which Files are Large.

Environment

  • Windows

    • Edition: 11 Pro
    • Version: 23H2
  • PowerShell

    • Edition: Desktop
    • Version: 5.1.22621.3958

Target Dir Example

C drive.

Conclusion

  1. Open PowerShell as administrator.
  2. Execute the following command.

    (Get-ChildItem "C:\" -Recurse -Force | Select-Object FullName, Length | Sort-Object Length -Descending)[0..2]
    
  3. Check the result.
    result

Structure

Execute the Most Basic Command.

Get-ChildItem is a cmdlet that specifies a dir path and retrieves information about files and directories at that path.

Specifying nothing is the same as specifying current dir.

Get-ChildItem
Enter fullscreen mode Exit fullscreen mode

Our target is C drive in this example, so we of course specify it.

Get-ChildItem "C:\"
Enter fullscreen mode Exit fullscreen mode

Include Hidden Items as well.

Hidden files are not reduced to zero size.
Thus, we need to retrieve those as well.

Get-ChildItem "C:\" -Force
Enter fullscreen mode Exit fullscreen mode

Select Information We Want.

As I write this, the default output properties do not include file size.
So we will use Select-Object to include it in the output.
Name property is included in the default output, so we will include that as well.

Get-ChildItem "C:\" -Force | Select-Object Name, Length
Enter fullscreen mode Exit fullscreen mode

We can use Get-Member to find out the names of the properties we can specify.
We can also add -MemberType to remove non-properties, and if we want to know the meaning of each element we can specify as -MemberType, we can check the other page.

Sort by Size.

Our goal is to identify files that are large.
For this purpose, the output should be ordered by size.

Get-ChildItem "C:\" -Force | Select-Object Name, Length | Sort-Object Length -Descending
Enter fullscreen mode Exit fullscreen mode

Filter Only Larger.

It is enough for us to check only large files.
In other words, we do not need to check small ones.

In this example, we consider displaying the top 3 files.

First, to convert the output as array, enclose the command in parentheses.

(Get-ChildItem "C:\" -Force | Select-Object Name, Length | Sort-Object Length -Descending)
Enter fullscreen mode Exit fullscreen mode

Once converted to an array, we can filter them by index.
Index starts at 0, so to display the top 3 files for example, we can specify 0, 1, and 2.

(Get-ChildItem "C:\" -Force | Select-Object Name, Length | Sort-Object Length -Descending)[0,1,2]
Enter fullscreen mode Exit fullscreen mode

Finally, we can more easily write index using Range Operator.

(Get-ChildItem "C:\" -Force | Select-Object Name, Length | Sort-Object Length -Descending)[0..2]
Enter fullscreen mode Exit fullscreen mode

Search Recursively.

Targeting the C drive means, of course, targeting within the folders on the C drive as well.
We can do it by adding option -Recurse.

(Get-ChildItem "C:\" -Recurse -Force | Select-Object Name, Length | Sort-Object Length -Descending)[0..2]
Enter fullscreen mode Exit fullscreen mode

Index can save PowerShell from overwork.
This is why we did not add this option in first step.

Get Absolute Path.

Since it is a recursive search, without the absolute path of the file, we cannot arrive to it.
To do so, we can replace Name with FullName.

(Get-ChildItem "C:\" -Recurse -Force | Select-Object FullName, Length | Sort-Object Length -Descending)[0..2]
Enter fullscreen mode Exit fullscreen mode

That's All.

Thank you so much.

Why Open PowerShell as Administrator?

Some files require admin auth to retrieve information.
Thus, by doing so, these are more accurate.

Top comments (0)