我对PowerShell还是个新手。当我的磁盘几次满的时候,我必须手动清理它。我尝试创建一个PowerShell脚本,它可以告诉我哪个文件夹的位置消耗了超过10 is的空间。
我用了这个-
$Directory = 'C:'
$FilesAndFolders =Get-ChildItem -Path $Directory -Recurse -ErrorAction SilentlyContinue -Depth 2
Foreach ($FileOrFolder in $FilesAndFolders) {
if ($FileOrFolder.Attributes -contains "Directory") {
#This is a folder - calculate the total folder size
$Length = (Get-ChildItem $FileOrFolder.FullName -Recurse |
Where-Object {$_.Attributes -notcontains "Directory"} |
Measure-Object -Sum -Property Length).Sum
$FileOrFolder | Select-Object Name, FullName, @{Name="Length"; Expression={$Length}}
}
}发布于 2021-09-12 20:16:44
您当前的代码提供了字节数。您必须执行(Get-ChildItem C:\Directory | Measure-Object -Property Length -sum) /1Gb才能将该值转换为GB。也是-强制查找隐藏或系统文件
发布于 2021-09-13 01:39:40
我在这里也有类似的东西。“‘dus”输出按大小排序的当前目录下的文件夹,“siz”输出文件。然后,您可以cd记录一个文件夹,并再次运行它们。它是基于我以前用过的unix别名。我把它们放到我的$profile里了。
function com { param([Parameter(ValueFromPipeline=$True)]
[int64]$number)
process { '{0:n0}' -f $number } }
function dus($dir=".") {
get-childitem -force -directory $dir -erroraction silentlycontinue -attributes !reparsepoint |
foreach { $f = $_
get-childitem -force -r $_.FullName -attributes !reparsepoint -ea 0 |
measure-object -property length -sum -erroraction silentlycontinue |
select @{Name="Name";Expression={$f}},Sum} | sort Sum |
select name,@{n='Sum';e={$_.sum | com}}
}
function siz() {
ls -file -force | select name,length |
sort length |
select name,@{n='Length';e={$_.length | com}}
}https://stackoverflow.com/questions/69154744
复制相似问题