0
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...

Mogistar
  • 1
  • 1
  • 2
    `-Path $OU.DistinghuishedName`, change `-OutputMode Single` into `-PassThru`. Your code would also benefit if you use [Splatting](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting) plus: where does variable `$username` come from? – Theo Jan 27 '23 at 12:04
  • Thank you for your response, I try use Splatting and I will comeback with response if it work or not. – Mogistar Jan 31 '23 at 10:09

0 Answers0