When creating users in a specific OU using PowerShell, you need to ensure that the -Path
parameter in the New-ADUser
cmdlet specifies the correct distinguished name (DN) of the target OU. Below is a sample script to create a user in the "Sydney Users" OU under the "Sydney" OU.
# Define the Distinguished Name (DN) of the parent and child OUs
$parentOU = "OU=Sydney,DC=yourdomain,DC=com"
$childOU = "OU=Sydney Users,$parentOU"
# Example user details
$userFirstName = "John"
$userLastName = "Doe"
$userSamAccountName = "jdoe"
$userPassword = ConvertTo-SecureString "P@ssword123" -AsPlainText -Force
# Create a new user in the "Sydney Users" OU
New-ADUser `
-GivenName $userFirstName `
-Surname $userLastName `
-SamAccountName $userSamAccountName `
-UserPrincipalName "$userSamAccountName@yourdomain.com" `
-Path $childOU `
-AccountPassword $userPassword `
-Enabled $true `
-PasswordNeverExpires $false `
-ChangePasswordAtLogon $true
Explanation:
- $parentOU: Specifies the distinguished name (DN) of the parent OU ("Sydney").
- $childOU: Specifies the distinguished name (DN) of the child OU ("Sydney Users"), which is a sub-OU under the "Sydney" OU.
- New-ADUser: Creates the user in the specified OU (
-Path $childOU
).
Important Notes:
- Replace
DC=yourdomain,DC=com
with the actual domain components of your environment. - Make sure the "Sydney" and "Sydney Users" OUs exist in Active Directory before running the script.
- If the user is still being created in the default
Users
container, verify that the-Path
parameter is correctly set and that there are no typos in the DN.
This script will create the user "John Doe" in the "Sydney Users" OU. Adjust the details as needed to match your specific requirements.