Post

Create AD users from a CSV-file

Here is an example of a script to create AD users from a CSV-file and assign a temoporary password.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
param
(
    [parameter(Mandatory = $true)]
    $infile,
    $output
)
 
$OU = "OU=Users,DC=PSLABB,Dc=local"
 
$users = Import-Csv -Path $infile
 
foreach ($user in $users)
{
    $rand = Get-Random -Minimum 1000 -Maximum 9999
    $pwd = "Temp$rand"
    $name = "$($user.givenname)" + " " + $($user.surname)
     
    New-ADUser -Name $name -GivenName $user.Givenname -Surname $user.Surname -SamAccountName $user.username -user $user.username -Description "Tempuser" -path $ou -enabled $true -AccountPassword (convertTo-securestring -AsPlainText "Temp$rand" -Force)
     
    $details = @{
        'Name'= $name;
        'username'  = $($user.username);
        'Password'     = $pwd
    }
    $results = New-Object PSObject -Property $details
    $results | Export-Csv -Path $output -NoTypeInformation -Append
     
}
This post is licensed under CC BY 4.0 by the author.