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

如何将cmd中dir命令输出作为PowerShell的Get-ChildItem路径参数

实现将CMD命令输出作为PowerShell Get-ChildItem参数的方法

方法1:CMD环境下循环传递单个文件路径

直接在CMD中用for /f捕获dir命令输出的文件完整路径,逐个传给PowerShell处理:

for /f "delims=" %f in ('dir /b/s/a-d screenshot_37.png') do powershell -command "& {Get-ChildItem -Path '%f' -Force}"
  • 说明:/a-d参数确保只返回文件(排除目录);如果是在批处理脚本中,变量需写成%%f而非%f。

方法2:直接用PowerShell完成搜索(更高效)

无需调用CMD,PowerShell本身的Get-ChildItem就能实现文件搜索并查看属性,直接执行以下命令:

Get-ChildItem -Path . -Filter "screenshot_37.png" -Recurse -Force | Select-Object FullName, Length, CreationTime, LastWriteTime
  • 说明:-Recurse递归搜索所有子目录,-Force包含隐藏文件;Select-Object可指定要查看的属性,按需调整即可。

方法3:CMD中批量传递所有文件路径给PowerShell

若需一次性传递所有找到的文件路径,可借助延迟变量扩展收集路径后传给PowerShell:

setlocal enabledelayedexpansion
set "filePaths="
for /f "delims=" %f in ('dir /b/s/a-d screenshot_37.png') do set "filePaths=!filePaths! '%f'"
powershell -command "& {Get-ChildItem -Path @(%filePaths%) -Force}"
endlocal

原示例命令的问题说明

你给出的示例中"dir output"是固定字符串,并非dir命令的真实输出,因此无法生效,必须通过上述方式捕获实际文件路径再传递。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 22:42:11