Set your Powershell scripts to always use the closest Domain Controller

When writing a new script, I like to make sure I know which DC my AD cmdlets are talking to. I do this by using the -Server flag, but rather than hard-coding a specific DC into my scripts, I always use the closest writable DC by starting all my scripts with this line:

$domainController  = (Get-ADDomainController -Writable -Discover -NextClosestSite).name

You’ll then specify this DC when you’re making your AD calls:

Get-ADUser -identity 'Benjamin Atman' -server $domainController

Starting your scripts this way will ensure you’re always talking to the closest DC, and that you always know which one you’re talking to.

One thought on “Set your Powershell scripts to always use the closest Domain Controller

  1. Chanell Miya

    Submit graduate study in Queen Mary College of
    England also offers opportunity to develop abilities by way of scholar neighborhood action initiatives or
    volunteering opportunities run by way of accredited volunteering programme called Provide https://math-problem-solver.com/ .
    This was all about how to cut back fractions.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please do some math to prove you\'re a human: * Time limit is exhausted. Please reload the CAPTCHA.