Perrypackettracer / Powershell-scripts-to-use-in-an-active-directory

0 stars 0 forks source link

Import users to active directory #1

Open Perrypackettracer opened 8 months ago

Perrypackettracer commented 8 months ago

xImport-Module ActiveDirectory

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null

$dialog = New-Object System.Windows.Forms.OpenFileDialog $dialog.InitialDirectory = $StartDir $dialog.Filter = "CSV (.csv)| .csv" $dialog.ShowDialog() | Out-Null

$CSVFile = $dialog.FileName

if ([System.IO.File]::Exists($CSVFile)) { Write-Host "Importing CSV..." $CSV = Import-Csv -LiteralPath "$CSVFile" } else { Write-Host "File path specified was not valid" Exit }

$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name $EmailDomain = "$Domain"

ForEach ($user in $CSV) { $fname = $user.'First Name' $lname = $user.'Last Name' $OUpath = $user.'Organizational Unit' $Username = "$fname.$lname" -replace '\s+', ''
$UserPrincipalName = "$Username@$EmailDomain"

New-ADUser -Name "$fname $lname" -GivenName $fname -Surname $lname -UserPrincipalName $UserPrincipalName -Path $OUpath -ChangePasswordAtLogon $true -AccountPassword $SecurePassword -Enabled $([System.Convert]::ToBoolean($user.Enabled))

echo "Account Created for $fname $lname in $OUpath with UserPrincipalName $UserPrincipalName"

}