Some environments have multiple domains in a single Active Directory forest. Working with Powershell in the same domain as your user account is easy, but what do you do when you need to run Powershell commands in one of the other domains? Change context. Powershell can traverse AD as a file system, and this allows an easy way to change the context for your script. Keep in mind, that to be able to load the AD Powershell module, you need to install the RSAT tools on the workstation that you are using to run your commands.
The following code will mount your current domain as a drive:
Import-Module ActiveDirectory New-PSDrive -Name "ADDomain" -PSProvider ActiveDirectory -root "" cd ADDomain: cd "DC=domain,DC=com"
If you want to mount a different domain in the forest, you need to point to a domain controller for the desired domain:
New-PSDrive -Name "OtherDomain" -PSProvider ActiveDirectory -root "" -server OtherDC.altdomain.com
Just remember, the Powershell instance that you are running must have permissions to access AD in the other domain. Typically in AD, domains in the same forest have trust relationships setup and that means you can use RunAs to open Powershell as an admin account from the alternate domain if your account doesn’t have admin privileges.
Leave a comment