PowerShell how to find the largest file in a partition (find files and sort by size) _powershell

Source: Internet
Author: User

This article describes a PowerShell command to use the first few files that occupy the largest space under a directory or disk (such as D disk). In other programming languages, this task may require large sections of code, and in PowerShell we only need one line.

When we find that a disk is out of space, as an administrator, we have to do some cleanup on the useless files on the disk. In the face of this work, we have to think about what is useless files, which is a large footprint of the file. In the actual process, we are not likely to spend a lot of time and effort to get rid of all the useless files, so the best practice is to find some of the largest footprint of the files, and then to determine what is useless. These occupy a large space, and useless files clean up, the problem of insufficient disk space suddenly dissolved.

Okay, now let's imagine how to find a directory or the largest file on the disk. One of the simplest ideas is to set up a very large array and then iterate through all the files and subfolders under the directory and save the names, paths, size of space, and so on to the array. Finally, the array is sorted to get the inverted list of the largest file. Understand the development of the brother should be able to feel the difficulty and complexity of the problem. Anyway, it's not easy!

Let's look at how to solve this problem in PowerShell.
The first step is to get a list of all the files under D disk. This is very simple, there is dir (ie Get-childitem) This cmdlet can be implemented.

Copy Code code as follows:
Dir d:\-recurse

In the second step, the resulting list is sorted in reverse order by file size.
Copy Code code as follows:
Dir d:\-recurse | Sort-object-descending length

Note that this is used to give Sort-object the first step of the file list as an input parameter, and then sort-object to the length (the size of the file footprint property, which is the property in the list object obtained in the first step, It's not random. Descending order (-descending indicates reverse).
The third step, after sorting the list, extracts the first 10 items.
Copy Code code as follows:
Dir d:\-recurse | Sort-object-descending Length | Select-object-first 10

This step doesn't have to be explained.

Finally get the object list, in order to output good-looking, but also in the Select-object, control the output of some properties, such as only the output name and length. So finally you can get the following results:

Copy Code code as follows:
PS c:\users\splaybow> dir d:\-recurse | Sort-object-descending Length | Select-object Name,length-first 10

Name Length
----                                                                     ------
Cn_windows_7_ultimate_x64_dvd_x15-66 ... 3341268992
Install.wim 2928372216
Vs2010ultimtrial.iso 2446950400
Microsoft Office2007 Simplified Chinese full version. RAR 626917327
Boot.wim 214131351
QQ2013SP2.exe 59240120
Ksbrowser_4.2.25.5498.exe 48147320
Wps.4369.19.552.exe 45136112
Lbpack_pro.exe 44252624
wlan_qualcommatheros_win7_64_ver1000 ... 40988233


From this can be seen, PowerShell in the operation of files, disks and other aspects of the powerful, especially convenient use. One of the sorting function, the choice of features are used to make people feel comfortable, haha! Hongo feel that the need for friends can refer to the study.

About PowerShell a sentence to find out the D disk the largest 10 files, this article on the introduction so much, I hope to help you, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.