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

如何用PowerShell提取目录所有文件的Date Modified并导出到文本文件

PowerShell 遍历目录导出文件路径与修改时间脚本

基础通用版本

适用于大多数场景,直接替换路径即可使用:

# 替换为你的共享目录路径
$rootPath = "\\server\share\your-target-directory"
# 替换为你要保存的输出文件路径
$outputPath = "C:\output\file_mtime.csv"

Get-ChildItem -Path $rootPath -File -Recurse -Force | ForEach-Object {
    "`"$($_.FullName)`",`"$($_.LastWriteTime.ToString('yyyy-MM-dd HH:mm:ss'))`""
} | Out-File -FilePath $outputPath -Encoding UTF8

参数说明

  • -File:仅枚举文件对象,自动过滤文件夹
  • -Recurse:递归遍历所有子层级目录
  • -Force:强制读取隐藏、系统属性的文件,无相关需求可删除该参数
  • 输出内容默认用双引号包裹字段,避免路径中包含逗号导致CSV格式错误
  • 时间格式可自行调整,例如添加毫秒可修改为 'yyyy-MM-dd HH:mm:ss.fff'
  • 需要兼容Excel打开的话,可将编码参数修改为 -Encoding UTF8BOM

大目录优化版本

如果需要处理十万级以上的超大文件量,使用.NET静态方法可以大幅提升遍历速度:

$rootPath = "\\server\share\your-target-directory"
$outputPath = "C:\output\file_mtime.csv"

$streamWriter = [System.IO.StreamWriter]::new($outputPath, $false, [System.Text.Encoding]::UTF8)

foreach ($fileFullPath in [System.IO.Directory]::EnumerateFiles($rootPath, "*", [System.IO.SearchOption]::AllDirectories)) {
    $lastWriteTime = [System.IO.File]::GetLastWriteTime($fileFullPath).ToString('yyyy-MM-dd HH:mm:ss')
    $streamWriter.WriteLine("`"$fileFullPath`",`"$lastWriteTime`"")
}

$streamWriter.Flush()
$streamWriter.Close()

注意:如果共享目录有权限限制导致部分路径无法访问,可在遍历逻辑中添加try-catch块捕获异常,避免脚本中断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 17:48:03