循环文件并运行命令 [英] Loop on files and run command

查看:80
本文介绍了循环文件并运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PowerShell,我需要循环访问文件夹中的所有文件,并为每个文件运行一个命令.

Using PowerShell, I need to loop on all the files in a folder and for each file run a command.

例如:

在文件夹C:\Users\Administrator\Documents\scripts中,我具有以下文件:user_1.csv,user_2.csv,... user_N.csv

In folder C:\Users\Administrator\Documents\scripts I have these files: user_1.csv, user_2.csv, ... user_N.csv

对于每个文件,我需要运行以下命令:

For each file I need to run this command:

Import-Csv .\user_N.csv | New-ADUser -Enabled $True -AccountPassword (ConvertTo-SecureString Pass123 -AsPlainText -force)

此命令应该在循环中,该循环迭代文件夹中的所有文件 有什么建议吗?

This command should be inside a loop that iterates all the files in the folder Any suggestion?

推荐答案

使用Get-ChildItem cmdlet检索所有csv文件并将其通过管道传输到Import-CSV cmdlet:

Use the Get-ChildItem cmdlet to retrieve all csv files and pipe it to the Import-CSV cmdlet:

 Get-ChildItem -Path 'C:\Users\Administrator\Documents\scripts' -Filter 'user_*.csv' |
    Import-Csv | 
    New-ADUser -Enabled $True -AccountPassword (ConvertTo-SecureString Pass123 -AsPlainText -force)

这篇关于循环文件并运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆