This content originally appeared on DEV Community and was authored by box
Environment
-
Windows
- Edition: 11 Pro
- Version: 23H2
-
- Edition: Desktop
- Version: 5.1.22621.3958
Target Dir Example
C drive.
Conclusion
- Open PowerShell as administrator.
-
Execute the following command.
(Get-ChildItem "C:\" -Recurse -Force | Select-Object FullName, Length | Sort-Object Length -Descending)[0..2]
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
Our target is C drive in this example, so we of course specify it.
Get-ChildItem "C:\"
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
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
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
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)
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]
Finally, we can more easily write index using Range Operator.
(Get-ChildItem "C:\" -Force | Select-Object Name, Length | Sort-Object Length -Descending)[0..2]
Search Recursively.
Targeting the C drive means, of course, targeting within the folders on the C drive.
We can do it by adding option -Recurse.
(Get-ChildItem "C:\" -Recurse -Force | Select-Object Name, Length | Sort-Object Length -Descending)[0..2]
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]
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.
This content originally appeared on DEV Community and was authored by box
box | Sciencx (2024-09-07T12:17:15+00:00) Identify Which Files are Large.. Retrieved from https://www.scien.cx/2024/09/07/identify-which-files-are-large/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.