首页 > 解决方案 > 您可以在运行 Get-ChildItem 以编译文件夹大小列表时写入过滤器以排除某些文件夹吗?

问题描述

我需要在一个目录中获取一组子文件夹的 csv 文件,该文件标记每个文件夹及其大小(最好以 GB 为单位)。

到目前为止,我有这个:

$targetPath = '\\folderpath\users'
(Get-ChildItem $targetPath -Recurse | Measure-Object Length -Sum).Sum

目录中的每个子文件夹中都有一个文件夹,我没有权限/访问权限,因此每次运行脚本时,都会收到拒绝访问错误。

文件夹路径如下所示:

'\\folderPath\users\.V6'

是否可以为上述文件夹写入排除项,并在收集用户文件中每个文件夹的大小时让脚本跳过它?文件夹的大小很小,因此不会对结果产生影响。

标签: powershell

解决方案


Get-ChildItem 具有 -Include 和 -Exclude 参数。

-Exclude <String[]> 以字符串数组的形式指定此 cmdlet 在操作中排除的一个或多个项目。此参数的值限定了 Path 参数。输入路径元素或模式,例如 *.txt。允许使用通配符。

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-ChildItem).Parameters
(Get-Command -Name Get-ChildItem).Parameters.Keys
Get-help -Name Get-ChildItem -Examples
# Results
<#
Get-ChildItem
Get-Childitem -System -File -Recurse
Get-ChildItem -Attributes !Directory,!Directory+Hidden
dir -att !d,!d+h
dir -ad
Get-ChildItem -File -Attributes !ReadOnly -path C:\ps-test
get-childitem . -include *.txt -recurse -force
get-childitem c:\windows\logs\* -include *.txt -exclude A*
get-childitem -name
#>
Get-help -Name Get-ChildItem -Full
Get-help -Name Get-ChildItem -Online

推荐阅读