Identify Which Files are Large.

box - Sep 7 - - Dev Community

Environment

  • Windows

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

    • Edition: Desktop
    • Version: 5.1.22621.4111

Target Dir Example

C drive.

Conclusion

  1. Open PowerShell as administrator.
    Some files require admin auth to retrieve info.
    Thus, by doing so, these are more accurate.

  2. Execute the following command.

    Get-ChildItem "C:\" -Recurse -Force 2>$null | Select-Object FullName, Length | Sort-Object Length -Descending | Select-Object -First 3
    
  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.
And we can use -First option of Select-Oblect.

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

By applying this after Sort-Object, we can retrieve the first elements for the array after it has been ordered.

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 | Select-Object -First 3
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 | Select-Object -First 3
Enter fullscreen mode Exit fullscreen mode

Supress Premission Denied.

There are some items that cannot be retrieved.
Their absolute paths are revealed by the error stream, but the size is not written of course.
So it is natural for us to try to hide them.

Error stream is assigned the number 2 and we can have them sent to a specified file by the > redirection operator.
Furthermore we can discard them by specifying $null as specified file.

Get-ChildItem "C:\" -Recurse -Force 2>$null | Select-Object FullName, Length | Sort-Object Length -Descending | Select-Object -First 3
Enter fullscreen mode Exit fullscreen mode
. . . . .