Powershell,查看所有驱动器 [英] Powershell, look through all drives
问题描述
我是 Powershell 的新手,我尝试在网上找到解决问题的方法,但似乎找不到.基本上我需要写一些东西,让 powershell 查看所有驱动器和目录以找到以下内容:
I'm new to Powershell and I've tried finding a solution to my problem online but I can't seem to find one. Basically I need to write something that will let powershell look through all drives and directories to find the following:
文件总数(不包括文件夹)、最大文件大小、平均文件大小、总文件大小
total number of files (exclude folders), largest file size, average file size, total file size
这是我目前所写的内容:
Here's what I've written so far:
$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 盘中查找.我不知道是否有办法查看我的计算机而不是特定驱动器,但我似乎找不到方法.任何帮助表示赞赏.
The issue is that it only looks in the C drive. I don't know if there's a way to look through my computer instead of specific drives but I couldn't seem to find a way. Any help is appreciated.
推荐答案
使用 "..."
定义一个字符串,因此在您的示例中,您尝试遍历字符串.
With "..."
you define a string, so in your example you try to loop over a string.
试试这个:
$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
循环.
Note! I didn't chang your Get-ChildItem
code (except the path where it will search). I only wrote the foreach
loop.
这篇关于Powershell,查看所有驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!