PowerShell – Remote Session to Domain Controller

Sometimes you need to run Active Directory scriptlets in PowerShell, but logging into your DC takes too long, there are too many RDCs already, or you’re just lazy.  Regardless, the following script will allow you to initiate a remote PowerShell session to your DC and import the ActiveDirectory module, so you can perform AD administration on your workstation.  If this script is saved in your environment path (e.g. C:\Windows\System32), you can call it at anytime from PowerShell by just typing the script name (I called mine RemAD.ps1 and put it at the beginning of AD scripts as “.\RemAD.ps1”).  Obviously you will need to be logged in as a user that has login rights to the DC.  Also, you will need to login to your DC at least once and run Enable-PSRemoting in PowerShell before remote sessions are possible.

I should point out that this method adds the prefix “Rem” to your standard PowerShell scriptlets (e.g. you would call Get-RemADDomainController instead of Get-ADDomainController in your remote session).  The prefix is not necessary, but using it might reduce confusion.

$RemServer = "DC01"
$s = new-pssession -computer $RemServer
Invoke-Command -session $s -script { Import-Module ActiveDirectory }
Import-PSSession -session $s -module ActiveDirectory -prefix Rem

One Comment

Leave a Reply