【问题标题】:Search computer for .docx, .xls files using PowerShell使用 PowerShell 在计算机中搜索 .docx、.xls 文件
【发布时间】:2016-04-14 01:46:09
【问题描述】:

我是 PowerShell 新手,在尝试定位某些类型的文件(.doc、.docx、.xls、.xlsx)、将文件名和大小(按文件扩展名分组)输出到文本文件时遇到困难,以及还包括每个文件扩展名的文件总数和文件总大小。

我目前的代码是:

$Report_File_Destination = "C:\Users\StayPositibve\Desktop\testing20.txt"

$path = ".\*"

Get-ChildItem $path -Include *.doc, *.docx, *.xls, *.xlsx -Recurse | Group-Object Extension -NoElement | Out-File $Report_File_Destination -Append

每次运行此代码时,我都会收到 Get-ChildItem 访问被拒绝(我以管理员身份运行 PowerShell)。我究竟做错了什么?感谢您的帮助!

【问题讨论】:

    标签: powershell


    【解决方案1】:

    这是因为它存在一些不允许您浏览的路径。您可以尝试使用 Get-ChildItem 的 -ErrorAction Ignore -Force 选项来忽略这些错误或强制访问用户无法访问的文件,例如隐藏文件或系统文件。在旧版本的 PowerShell 中,您可以测试 -ErrorAction SilentlyContinue

    Get-ChildItem $path -Include *.doc, *.docx, *.xls, *.xlsx -Recurse -ErrorAction Ignore -Force
    

    【讨论】:

    • 感谢@JPBlanc 的帮助。我尝试添加 -ErrorAction Ignore -Force 选项;但是我仍然收到错误代码。我注意到的另一件事是,当使用 |组对象扩展,它不会将找到的文件复制到我的 txt 文件“testing20.txt”,而是在 txt 文件中为空白。抱歉没有正确使用正确的代码编辑,第一次使用 stackoverflow,并且不知道如何使代码看起来像你的。
    • 如果您使用的是旧版本的 PowerShell,请尝试 -ErrorAction SilentlyContinue
    • -NoElement 解释了您丢失文件的事实。
    • 非常感谢@JPBlanc 的帮助。 -ErrorAction SilentlyContinue 和您对 -NoElement 的建议帮助很大!
    猜你喜欢
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2014-10-27
    • 1970-01-01
    • 2011-03-22
    • 2013-09-24
    相关资源
    最近更新 更多