在 Windows Powershell 中的多个子文件夹中执行命令

在 Windows Powershell 中的多个子文件夹中执行命令

我所在的文件夹中有 24 个子文件夹。子文件夹名为 Angle1、Angle2、...、Angle24。在每个子文件夹中,我都想执行以下命令:

Get-ChildItem PKA.dump -r | Sort-Object $_ | ForEach-Object { Get-Content $_ | Select -Index 19 } > output.txt

有没有办法使用 Windows PowerShell 来做到这一点?

注意,我不想在子文件夹中执行命令。

这并没有完全实现我的愿望:

Get-ChildItem/*/ PKA.dump -Recurse | Sort-Object $_ | ForEach-Object { Get-Content $_ | Select -Index 19 } > output.txt

答案1

你可以使用这个:

在父级中创建一个 PowerShell 对象(即script.ps1持有Get-ChildItem PKA.dump -r | Sort-Object $_ | ForEach-Object { Get-Content $_ | Select -Index 19 } > output.txt),然后使用以下命令启用未签名的执行:Set-ExecutionPolicy Unrestricted从管理 PowerShell

将其复制并粘贴到批处理文件中:

copy script.ps1 Angle1
cd Angle1
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle2
cd Angle2
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle3
cd Angle3
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle4
cd Angle4
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle5
cd Angle5
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle6
cd Angle6
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle7
cd Angle7
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle8
cd Angle8
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle9
cd Angle9
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle10
cd Angle10
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle11
cd Angle11
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle12
cd Angle12
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle13
cd Angle13
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle14
cd Angle14
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle15
cd Angle15
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle16
cd Angle16
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle17
cd Angle17
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle18
cd Angle18
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle19
cd Angle19
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle20
cd Angle20
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle21
cd Angle21
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle22
cd Angle22
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle23
cd Angle23
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle24
cd Angle24
powershell .\script.ps1
del script.ps1
cd ..
exit

保存并以管理员身份运行该脚本。

例如:script.bat右键单击,Run as Administrator

相关内容