我是Powershell的新手,我尝试在网上找到问题的解决方案,但我似乎无法找到解决方案。基本上我需要写一些东西,让powershell查看所有驱动器和目录,以找到以下内容:
文件总数(不包括文件夹),最大文件大小,平均文件大小,文件总大小
这是我到目前为止所写的内容:
$source = "get-psdrive"
#attempt at looking through all drives/directories which didn't work
foreach($a in $source) {
#counts files in given directory
Get-ChildItem -Recurse -File -ErrorAction SilentlyContinue -Force |
Measure-Object |
ForEach-Object { $_.Count }
#select largest file in given directory
Get-ChildItem -Recurse -File -ErrorAction SilentlyContinue -Force |
Sort-Object Length -Descending |
Select-Object -First 1
#get average file size in a given directory
Get-ChildItem -Recurse -File -ErrorAction SilentlyContinue -Force |
Measure-Object -Property Length -Average
#sum of file sizes in given directory
(Get-ChildItem -Recurse -File -ErrorAction SilentlyContinue -Force |
Measure-Object -Sum Length).Sum
}
问题是它只能在C盘中查找。我不知道是否有办法通过我的电脑而不是特定的驱动器,但我似乎无法找到方法。任何帮助表示赞赏。
答案 0 :(得分:2)
使用"..."
定义一个字符串,因此在您的示例中,您尝试循环一个字符串。
试试这样:
$Drives = Get-PSDrive -PSProvider 'FileSystem'
foreach($Drive in $drives) {
#counts files in given directory
Get-ChildItem -Path $Drive.Root -Recurse -File -ErrorAction SilentlyContinue -Force |
Measure-Object |
ForEach-Object { $_.Count }
#select largest file in given directory
Get-ChildItem -Path $Drive.Root -Recurse -File -ErrorAction SilentlyContinue -Force |
Sort-Object Length -Descending |
Select-Object -First 1
#get average file size in a given directory
Get-ChildItem -Path $Drive.Root -Recurse -File -ErrorAction SilentlyContinue -Force |
Measure-Object -Property Length -Average
#sum of file sizes in given directory
(Get-ChildItem -Path $Drive.Root -Recurse -File -ErrorAction SilentlyContinue -Force |
Measure-Object -Sum Length).Sum
}
请注意!我没有更改你的Get-ChildItem
代码(除了它将搜索的路径)。我只写了foreach
循环。