Move newly created AD object to particular OU after accepting input

Hi I am new to powershell, trying to create a script to create new AD accounts and then immediately move the object to a particular OU based on user input.

Below is the snipped of code, I am trying to call the firstname and lastname which the user has input previously, when finding that it moves the object to the specified OU, how can I do this?

new-ADUser -Name "$firstname $lastname" -givenname "$lastname" -surname "$firstname" -Description "Created - $ticket" -Displayname "$firstname $lastname" -ChangePasswordAtLogon 1 -UserPrincipalName "$firstname.$lastname@---"

Start-Sleep -s 5
$dept = read-host "Please enter user dept (CORP/CA/CS/FRAUD/DC)"
$role = read-host "Please confirm role (AGENT/KE/TM)"
switch ($dept,$role)
{
'CA,AGENT' {
Get-ADUser -Identity $firstname.$lastname | Move-ADobject -TargetPath "OU=Agents,OU=Customer Assistance,OU=View Users,DC=---,DC=internal"
}
}

Akshay, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

A much better option would be to create the AD object where you want it to be right away. Therefor you’d provide the path for the cmdlet New-ADUser. That would make the step of moving it obsolete. If you insist to do it this way you may save the newly created AD object in a variable. This you can easily move wherever you want. :wink:

[quote quote=238358]Akshay, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.

In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.

Thanks in advance.

A much better option would be to create the AD object where you want it to be right away. Therefor you’d provide the path for the cmdlet New-ADUser. That would make the step of moving it obsolete. If you insist to do it this way you may save the newly created AD object in a variable. This you can easily move wherever you want. 😉[/quote]

Thanks so much, very helpful.