powershell - 计算目录中PDF文件的数量并输出到.csv

标签 powershell

我正在尝试报告目录中pdf文件的数量。下面的代码工作正常,但是我向其中添加了Export-Csv,并且输出不起作用。文件已创建,但计数错误。我在输出文件的单元格1A中收到“#TYPE System.Int32”,而不是文件计数..不知道为什么。

(get-ChildItem C:\Test\* -Filter *.pdf -Recurse).Count | Export-Csv C:\TEMP\Test.csv

最佳答案

当您具有带有属性和值的对象或哈希表时,Export-CSV效果更好。在这种情况下,您所拥有的只是一个数字,并且不知道列标题应该是什么。如果您想要的只是文件中的数字,请尝试以下操作:

(get-ChildItem C:\Test\* -Filter *.pdf -Recurse).Count | Set-Content C:\TEMP\Test.csv

但是,如果您确实想要csv文件或其他项目的示例,请尝试以下操作:
$HashTable = @{NumberOfPDFFiles = ((get-ChildItem C:\Test\* -Filter *.pdf -Recurse).Count)}
$HashTable | Export-csv C:\TEMP\Test.csv -NoTypeInformation

或者像这样的东西坚持一线主意
(get-ChildItem C:\Test\* -Filter *.pdf -Recurse).Count | 
    Select-Object @{n='PdfCount';e={$_}} | 
    Export-CSV C:\TEMP\Test.csv -NoTypeInformation

关于powershell - 计算目录中PDF文件的数量并输出到.csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27137588/

相关文章:

powershell - 为什么Write-Output在PowerShell类方法中不起作用?

powershell - 以制表符分隔格式将 PowerShell 输出导出到文本文件

powershell - 注册 PSRepository 和安装 VMWare PowerCLI 模块的问题

Powershell - 获取 AD 用户的姓名首字母

powershell - 从 Azure DevOps 获取所有项目下的所有用户

powershell - Powershell如何自动加载模块?

Powershell 命名管道安全性?

php - 使用 powershell 为 wampserver 创建虚拟主机

powershell - 从文本文件的文件名列表中将文件从一个文件夹移动到另一个文件夹

powershell - 如何将值传递给嵌套的 ForEach?