Tag Archives: CMDlet

Passing CSV files to powershell to perform bulk commands

Nice easy one for a Wednesday morning.
Again, you’ll need the quest Powershell ActiveRoles Management Plugin from here: http://www.quest.com/powershell/activeroles-server.aspx

For this i’ll be using the New-QADUser command but you can use any command you like. All the csv and code is doing before that command is populating some variables that are passed to the command.

To tell Powershell what CSV file to use, you use the Import-Csv command and assign to a variable, then pick out each row and assign each value to its own variable :

$List = Import-Csv C:\scripts\list.csv
ForEach ($entry in $list){ 
$firstname = $($entry.firstname)
$lastname = $($entry.lastname)
$ccg = $($entry.ccg)
$code = $($entry.code)
$job = $($entry.job)
$name=$lastname+” “+$firstname
$username=$firstname+”.”+$lastname
$displayname=$name+” (DOMAIN)”
$upn=$username+”@domain.uk”
$password=”tgrefgbfdbfdshgtfsbgfd”
if ($ccg -eq “preston”) {
$ou = “Domain/OU1/Preston”
}
if ($ccg -eq “csr”) {
$ou = “Domain/OU2/C&SR”
}
if ($ccg -eq “wl”) {
$ou = “Domain/OU3/West Lancs”
}

The CSV contains the users name, CCG (OU Name), job title and Job Code. Each are assigned a variable, then other values such as Display name and username are constructed from the variables in the csv file. If I wanted to add more values from the csv, id create a new column (eg Phone number), then reference it in the code like this:

$phone = $($entry.phone)
 

The $phone at the start of the string is the variable within powershell. the “($entry” is the variable for the csv specified at the top of the code and “.phone)” is the column name in the csv.
Once all the variables have been specified that you want to use, you add the command:

New-QADUser -FirstName $firstname -LastName $lastname -Name $name -DisplayName $displayname -UserPrincipalName $upn -UserPassword $password -ParentContainer $ou -SamAccountName $username -Office $code -Description $job
 

To add an additional variable such as phone number, you would add “-PhoneNumber $phone” to the command.
Blank values in the CSV will add a blank value to the object in AD when the account is created.  PS1 download is below.

Powershell File

Sample CSV