Active-Directory – Nutzer via Script im Bulk mit Powershell anlegen

powershell wsus konfiguration01111

Mit Windows Server 2008R2 kommt das Addlet „New-ADUser“ für die Powershell bei den Active Directory Verwaltungstools mit: Mit dessen Hilfe kann man ganz einfach und bequem Nutzer im Active Directory anlegen. Wer mehr im Detail über New-ADUser erfahren möchte, wird im Technet fündig: https://technet.microsoft.com/de-de/library/ee617241.aspx

Das folgende Script führt New-ADUser in einer Zählschleife aus, wobei der Platzhalter „XX“ durch die laufende Nummer ersetzt wird. So kommen nach dem Lauf 10 Active-Directory Konten raus die wie folgt aussehen: buha.azubi01,…,buha.azubi10

$ad_count_of_users_to_create=10
$ad_acc_firstname=""
$ad_acc_surname="buha.azubiXX"
$ad_acc_displayname=$ad_acc_surname
$ad_acc_SamAccountName=" buha.azubiXX"
$ad_acc_UserPrincipalName=" buha.azubiXX"

$ad_acc_password="pa$$w0rd"
$ad_acc_department="Buchhaltung"
$ad_acc_description="Account fuer die Azubis der Buchhaltung "

$ad_domainname_short="meinefirma"
$ad_domainname_long=" meinefirma.local"
$ad_path="OU=Azubis,OU=User,OU= "+$ad_domainname_short+",DC="+$ad_domainname_short+",DC=meinefirma,DC=de"

### load AD Module 
Import-Module ActiveDirectory

for ($i=1;$i -le $ad_count_of_users_to_create; $i++) {

    $current_number=$i
    if($current_number -lt 10){$current_number="0"+$current_number}

    $tmp_ad_acc_surname=$ad_acc_surname -replace 'XX',$current_number

    $tmp_ad_acc_displayname=$ad_acc_displayname -replace 'XX',$current_number

    $tmp_ad_acc_SamAccountName=$ad_acc_SamAccountName -replace 'XX',$current_number

    $tmp_upn = $tmp_ad_acc_SamAccountName+“@”+$ad_domainname_long 
    
    write-host "`n`nSamAccountName: "$tmp_ad_acc_SamAccountName"`n Name: "$tmp_ad_acc_SamAccountName"`n UserPrincipalName: "$tmp_upn"`n firstname: "$ad_acc_firstname"`n Surname: "$tmp_ad_acc_surname"`n DisplayName: "$tmp_ad_acc_displayname"`n Description: "$ad_acc_description"`n Department: "$ad_acc_department"`n Path: "$ad_path"`n AccountPassword: "$ad_acc_password
    
    New-ADUser -SamAccountName $tmp_ad_acc_SamAccountName -UserPrincipalName $tmp_upn -Name $tmp_ad_acc_SamAccountName -SurName $tmp_ad_acc_surname -DisplayName $tmp_ad_acc_displayname -Department $_.Department -Path $ad_path -Description $ad_acc_description -AccountPassword (ConvertTo-SecureString $ad_acc_password -AsPlainText -force) -Enabled $True -PasswordNeverExpires $True -PassThru 

} 

weitere Beiträge zum Thema Powershell und nützliche Befehle