You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell 5.1目录统计命令输出至TXT文件为空求助

问题原因及解决办法
  • 问题根源:你使用的Write-Host是直接将内容输出到控制台,不会进入PowerShell的输出管道,因此>重定向符号无法捕获这些内容,导致生成的txt文件为空。
  • 解决方法:替换Write-Host为直接输出字符串(PowerShell默认会将字符串送入输出管道),或者使用Write-Output命令。

修改后的命令示例:

dir -Recurse | ?{ $_.PSIsContainer } | %{ "$($_.FullName) $( (dir $_.FullName | Measure-Object).Count )" } > list.txt

如果想提升命令效率(避免重复遍历目录),可以用更清晰的参数写法:

Get-ChildItem -Recurse -Directory | ForEach-Object {
    $fileCount = (Get-ChildItem -File -Path $_.FullName).Count
    "$($_.FullName) $fileCount"
} | Out-File list.txt

内容的提问来源于stack exchange,提问作者brink

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 14:52:03