PowerShell will automatically create a PSDrive for the Active Directory domain that the client is a member of. An additional PSDrive can be created for a different domain in another forest.
First, make sure that the Active Directory PowerShell module is loaded.
Now you can create the connection with the New-PSDrive commandlet.
The new PSDrive can be used in several ways. Lets say that you have a test AD forest that you want to manage with PowerShell from a host joined to your production AD forest. The test forest has a domain controller named “dc-test-01.test.domain.com” that is providing the AD web service. The following command will create a PSDrive named “TEST” that will be connected to the “test.domain.com” domain.
If you’re going to run this cmd-let from a script you will have to make sure that you include the “-Scope Global” switch. Otherwise the PSDrive will be created within the scope of the script and will not be available to you in the shell.
To use this PSDrive you can “cd” to the “TEST” PSDrive and then run the Active Directory modules as normal:
The second method is to provide the “server” switch with the name of the domain controller:
The following code will check to see if the drive exists prior to attempting creation of the new PSDrive