如何使用PowerShell递归搜索包括隐藏目录中的所有文件(包括隐藏文件)?

3
为了递归地搜索隐藏文件,我使用以下代码:
gci -Path C:\ -Filter part_of_filename* -Recurse -Force | where { $_.Attributes -match "Hidden"}

输出结果显示许多类似于这样(根据路径不同)的错误消息:

Get-ChildItem: 拒绝访问路径“C:\Documents and Settings”。位置 C:\Users\USERNAME\Documents\powershell\searchdisk.ps1:10 字符:5 + gci <<<< -Path C:\ -Filter part_of_filename* -Recurse -Force | where { $_.Attributes -match "Hidden" } + CategoryInfo:PermissionDenied: (C:\Documents and Settings:String) [Get-ChildItem],UnauthorizedAccessException + FullyQualifiedErrorId:DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

我需要一个PowerShell命令来递归搜索包括隐藏目录在内的任何目录,并显示所有名称为part_of_filename*(以本例为例)的文件,包括隐藏文件。
我已经以管理员身份在PowerShell ISE中运行了该命令。它无法搜索像C:\Windows\System32\LogFiles\WMI\RtBackup这样的目录。

1
这绝对需要以提升的权限运行。 - Matt
1
问题在于“Documents and Settings”不是一个目录,而是一个联接点。有一些NTFS技巧可以让程序相信该目录仍然存在,而实际上它只是指向“Users”的快捷方式。这里有一个关于这个主题的SO问题:https://dev59.com/TEzSa4cB1Zd3GeqPjyJi - Kenned
1个回答

9

你做得很对。只需在提权的控制台中运行它并删除过滤器即可。如果您不关心权限错误,请添加-ErrorAction SilentlyContinue

Get-ChildItem -Path C:\ -Filter lush* -Recurse -Force `
              -ErrorAction SilentlyContinue


Directory: C:\

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-arhs        25.09.2013     12:16       1208 lush.asx

lush.asx拥有ReadOnlyHiddenSystem属性。

你可能还想将其管道传送到| select Name, Length, Directory以摆脱那个不幸的Directory: C:\行。如果你希望在不包含文件名的情况下获取完整路径,还可以使用DirectoryName


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接