Câu lệnh tìm kiếm và sắp xếp các thư mục theo dung lượng từ lớn đến bé. Mục đích để kiểm tra thư mục nào chiếm nhiều dung lượng trên ổ đĩa.
Get-ChildItem "C:\Program Files (x86)" -Directory | ForEach-Object {
>> $size = (Get-ChildItem $_.FullName -File -Recurse -ErrorAction SilentlyContinue |
>> Measure-Object Length -Sum).Sum
>>
>> [PSCustomObject]@{
>> Folder = $_.FullName
>> SizeGB = [math]::Round($size / 1GB, 2)
>> }
>> } | Sort-Object SizeGB -Descending
Đoạn lệnh tìm kiếm và sắp xếp các thư mục theo dung lượng giảm dần, liệt kê tất cả các thư mục con.
Get-ChildItem "C:\Program Files" -Directory -Force -ErrorAction SilentlyContinue |
>> ForEach-Object {
>> $folder = $_
>>
>> $size = (Get-ChildItem $folder.FullName -File -Recurse -Force -ErrorAction SilentlyContinue |
>> Measure-Object Length -Sum).Sum
>>
>> [PSCustomObject]@{
>> SizeGB = [math]::Round($size / 1GB, 2)
>> Folder = $folder.FullName
>> }
>> } |
>> Sort-Object SizeGB -Descending |
>> Format-Table -AutoSize