I have a script that I made that will create active directory users. I want to export it to a .csv file with the headings Firstname,Lastname,Loginname,Password. Here is what I have so far:
$csvcontent = Import-CSV -Path c:\users\administrator\desktop\users.csv
foreach ($user in $csvcontent)
{
$Random = Get-Random -minimum 100 -maximum 999
$pwdlength = 10
$bytes = [byte[]][byte]1
$pwd = [string]""
$rng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
while (!(($PWD -cmatch "[a-z]") -and ($PWD -cmatch "[A-Z]") -and ($PWD -match "[0-9]"))){
$pwd = ""
for($i=1;$i -le $pwdlength;$i++)
{
$rng.getbytes($bytes)
$rnd = $bytes[0] -as [int]
$int = ($rnd % 74) + 48
$chr = $int -as [char]
$pwd = $pwd + $chr
}}
New-ADUser -Name ($user.Firstname+” “+$user.Lastname+"$Random") -GivenName $user.Firstname -Surname ($user.Lastname+"$Random") `
-SamAccountName ($user.Lastname+$user.Firstname.Substring(0,1)+"$Random") -UserPrincipalName ($user.Lastname+$user.Firstname.Substring(0,1)+"$Random"+“@nagara.ca”) `
-AccountPassword (ConvertTo-SecureString "$pwd" -AsPlainText -force) `
-Path 'OU=Test,DC=nagara,DC=ca' `
-PassThru | Enable-ADAccount
$info="$user.Firstname,$user.Lastname,$pwd"
$info | add-content .\accountinformation.csv
}