1

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

For example:

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?

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
user3472065
  • 1,259
  • 4
  • 16
  • 32

1 Answers1

2

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)
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172