Introduction
In this article, we will explore how to create new users in a different domain using PowerShell. Specifically, we will focus on creating a user in the domain contoso.com, while the manager of the new user is located in the domain example.com.
Prerequisites
To follow along with this article, you should have a basic understanding of PowerShell and Active Directory. Additionally, you should have access to an Active Directory environment with at least two domains set up.
Creating a New User in a Different Domain
To create a new user in a different domain, we will use the New-ADUser cmdlet. This cmdlet allows us to create new users in Active Directory, and we can specify the domain to create the user in using the -Credential parameter.
Example: Creating a New User in the Domain contoso.com
$password = ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ("contoso.com\Administrator", $password)
New-ADUser -Credential $credential -Name "John Doe" -GivenName "John" -Surname "Doe" -SamAccountName "jdoe" -UserPrincipalName "[email protected]" -AccountPassword $password -Enabled $true
In this example, we first create a secure password for the new user using the ConvertTo-SecureString cmdlet. We then create a PSCredential object using the New-Object cmdlet, which we will use to authenticate to the contoso.com domain. Finally, we use the New-ADUser cmdlet to create a new user in the contoso.com domain with the specified properties.
Adding a Manager to the New User
To add a manager to the new user, we can use the -Manager parameter of the New-ADUser cmdlet. However, since the manager is located in a different domain, we need to specify the manager's distinguished name (DN) instead of their user principal name (UPN).
Example: Adding a Manager to the New User
$managerDN = (Get-ADUser "[email protected]" -Server "example.com").DistinguishedName
New-ADUser -Credential $credential -Name "John Doe" -GivenName "John" -Surname "Doe" -SamAccountName "jdoe" -UserPrincipalName "[email protected]" -AccountPassword $password -Enabled $true -Manager $managerDN
In this example, we first use the Get-ADUser cmdlet to retrieve the manager's DN from the example.com domain. We then use this DN as the value of the -Manager parameter when creating the new user in the contoso.com domain.
-
To create a new user in a different domain using PowerShell, we can use the
New-ADUsercmdlet and specify the domain to create the user in using the-Credentialparameter. -
To add a manager to the new user, we can use the
-Managerparameter of theNew-ADUsercmdlet and specify the manager's DN instead of their UPN.