Операции с пользователями AD при помощи PS
Создание пользователей
Создать пользователя с указанным именем. Аккаунт будет отключен и для его включения необходимо задать пароль. Никакой другой информации заполнено не будет.
New-ADUser user
Создать активного пользователя с интерактивным вводом пароля в OU managers в домене arasaka.local:
New-ADUser -Name "Jack Robinson" -GivenName "Jack" -Surname "Robinson" -SamAccountName "J.Robinson" -UserPrincipalName "J.Robinson@enterprise.com" -Path "OU=Managers,DC=arasaka,DC=local" -AccountPassword(Read-Host -AsSecureString "Input Password") -Enabled $true
Более насыщенный параметрами вариант:
New-ADUser -Name "Jason Bourne" -GivenName "Jason" -Surname "Bourne" -SamAccountName "Jason-Bourne" -AccountPassword (ConvertTo-SecureString -AsPlainText “webdir123R” -Force) -ChangePasswordAtLogon $True -Company "Versacorp" -Title "CEO" -State "California" -City "San Francisco" -Description "Test Account Creation" -EmployeeNumber "45" -Department "Engineering" -DisplayName "Jason Bourne" -Country "US" -PostalCode "94001" -Enabled $True
Вывести пользователя, включив определенные свойства:
Get-ADUser -Identity Jason-bourne -Properties * | select name,samaccountname,company,title,department,city,state,country,description,employeenumber,postalcode
Создать пользователя, указав расширенные аттрибуты:
New-ADUser -Name "Jason Bourne" -Path "OU=NBC,DC=milkyway,DC=local" -GivenName "Jason" -Surname "Bourne" -SamAccountName "Jason-Bourne" -AccountPassword (ConvertTo-SecureString -AsPlainText “webdir123R” -Force ) -ChangePasswordAtLogon $True -DisplayName "Jason Bourne" -Enabled $True -OtherAttributes @{'extensionattribute1'="director";'carlicense'="LWG3852"}
Имя пользователя (не логин, а именно Display Name) должно быть уникальным в рамках одного OU, иначе возможны дупликаты DN.
А вот таким скриптом можно создать всех пользователей из файла CSV, имеющего определенный формат.
#Define path to your import CSV file location in a variable as shown in below line. $AD_Users = Import-csv C:\scripts\newusers.csv foreach ($User in $AD_Users) { $User_name = $User.username $User_Password = $User.password $First_name = $User.firstname $Last_name = $User.lastname $User_Department = $User.department $User_OU = $User.ou #Below if-else condition will check if the user already exists in Active Directory. if (Get-ADUser -F {SamAccountName -eq $User_name}) { #Will output a warning message if user exist. Write-Warning "A user $User_name has already existed in Active Directory." } else { #Will create a new user, if user is not available in Active Directory. #User account will be created in the OU listed in the $User_OU variable in the CSV file; it is necessary to change the domain name in the"-UserPrincipalName" variable in the script below. New-ADUser ` -SamAccountName $User_name ` -UserPrincipalName "$Username@example.com" -Name "$First_name $Last_name" -GivenName $First_name ` -Surname $Last_name ` -Enabled $True ` -ChangePasswordAtLogon $True ` -DisplayName "$Last_name, $First_name" ` -Department $User_Department ` -Path $User_OU ` -AccountPassword (convertto-securestring $User_Password -AsPlainText -Force) } }
Формат файла CSV для работы этого скрипта выглядит так:
Всё украдено отсюда: https://blog.netwrix.com/how-to-create-new-active-directory-users-with-powershell
Массовое перемещение
Выберем все отключенные учётки и переместим в другой OU:
Get-ADUser -Filter {Enabled -eq $false} | Move-ADObject -TargetPath "OU=disabled,DC=arasaka,DC=local"