How to specific OU automatticaly for new user in AD?

85 Views Asked by At
Import-Module ActiveDirectory

$firstname = Read-Host -Prompt "Please write firstname"
$lastname = Read-Host -Prompt "Please write lastname"

$SearchBase = "DC=Domain,DC=com"
$OUList = Get-ADOrganizationalUnit -SearchBase $SearchBase -Filter * -Properties Name,DistinguishedName | Select-Object -Property Name,DistinguishedName

$OU = $OUList | Out-GridView -Title "Select OU and Click OK" -OutputMode Single 



#Create the AD User
New-ADUser `
-Name "$firstname $lastname" `
-GivenName $firstname `
-Surname $lastname `
-UserPrincipalName "$firstname.$lastname".ToLower() `
-AccountPassword (ConvertTo-SecureString "Customs1!" -AsPlainText -Force) `
-Path $OU `
-ChangePasswordAtLogon 1 `
-Enabled 1 `
-DisplayName "$firstname $lastname" `
-SamAccountName "$firstname.$lastname".ToLower() 

  $timer = [diagnostics.stopwatch]::startnew()
  $erroractionpreference = "silentlycontinue"
  $mailboxsuccess = $false
  while (($timer.elapsed.totalseconds -lt 900) -and (!($mailboxsuccess)))
   {
   $error.clear()

   Enable-Mailbox -Identity $username -Alias $username -Database $mailboxdatabase
`your text`
   if($error.count -eq 0)
    {
    write-host "Mailbox successfully created"
    $mailboxsuccess = $true
    }

   else
    {
    write-host "." -nonewline
    start-sleep -s 30
    }
  
   }
  $timer.stop()
  $erroractionpreference = "continue"
  $secs = "{0:N2}" -f ($timer.elapsed.totalseconds)
  write-host "Process ran for: $secs seconds."


I try to create an user in specific OU automatically with grid table where I select an OU and click ok, but my program doesn't work, after run appear this.

Please write firstname: Test1
Please write lastname: Test2
....

I need help, thank you. So far I managed to create a user in a specific OU manually, but I don't want this, I want in automatically mode, I don't know how...

0

There are 0 best solutions below