How do I connect to the AD drive using PowerShell?
The Active Directory drive (AD:) in PowerShell gives administrators an easy way to explore AD from the command line, in much the same way you would list the directory contents of a hard disk using the DIR command in DOS. Let’s see how to connect to the AD drive and the commands used to navigate through it.
Connecting to the AD drive
- Log in to Windows Server 2012 with a user account that has permission to access AD and open PowerShell from the icon on the desktop Taskbar.
- Type Import-Module ActiveDirectory in the PowerShell window and press Enter.
- Now we need to set the working location to the AD drive. Type Set-Location AD: and press Enter. Notice that the PowerShell prompt now changes to PS AD: >.
- Type DIR and press Enter.
You’ll see a list of the forest’s partitions. To do anything useful, we need to connect to the domain partition using the set-location cmdlet.
- Type Set-Location “<distinguished name>” and press Enter.
The distinguished name (DN) of your domain can be seen in the output of the previous DIR command. The DNS name of my Active Directory domain is ad.contoso.com, therefore the domain’s DN is dc=ad,dc=contoso,dc=com. So the command should look like Set-Location “dc=ad,dc=contoso,dc=com”. Again the PowerShell prompt will change, in this example to PS AD:\dc=ad,dc=contoso,dc=com>.
- To drill further down into the AD hierarchy, use the Set-Location cmdlet to change the working location to the Users container. Type Set-Location “cn=users” and press Enter.
Note that if you want to change the working location to an Organizational Unit (OU) instead of a container, change cn to ou. For example, to connect to the HR OU, the command might look like Set-Location “ou=hr users”.
In exactly the same way you navigate back up a directory tree in DOS, you can use the set-location command to move back up a level. For example, to move from cn=users,dc=ad,dc=contoso,dc=com up one level to dc=ad,dc=contoso,dc=com, type set-location followed by two dots in the PowerShell window, like so: set-location ..Advertisement
Saving a directory location
If you want to move in and out of the AD drive, or move around the AD drive but need to frequently return to a particular location, you can use the push-location and pop-location cmdlets:
- To save the current working location, type Push-Location and press Enter.
- Now let’s return to the C drive by typing Set-Location C: and press Enter.
- To quickly return to the previously saved location, type Pop-Location and press Enter.