Finding ADDomainController with wild card

I have been working on a script to connect to create AD Objects for a company with a number of remote sites. Because some of the sites have very slow links replication can be a pain so I thought to connect to the DC in the particular site and create it there. In looking at the docs on Get-ADDomainController, however, there seems to be no support for wildcards. I tried to do a two-step, something like this:

$aDCs = Get-ADDomainController -Filter * | Select-Object Name

ForEach ($sDC in $aDCs) {
    If ($sDC.Name -like "ATL") {
        Write-Host $sDC.Name

to grab the domain controller “ATL-DC03”, but that does not work. I can see ATL-DC03 in $aDCs, and if I remove the if statement if prints all of the DC names, including the one I am after, but I cannot seem to match it and get only that one. I have tried -like, -contains, and just about every other comparison I can think of, but no joy. Is there an easy way to get this done? Or better yet a way to do the comparison right in the Get-ADDomainController line? I tried a couple variances on this:

Get-ADDomainController -Filter * | Where-Object ($_.Name -like "ATL")

But similarly no luck. Any suggestions would be most welcome

This is because if you are looking for all DC’s with ATL in the as the name, you really need to use ‘ATL*’ not just ‘ATL’.
You can also use RegEx matches directly and do this in one line.

For example, try —
$aDCs = (Get-ADDomainController -Filter * | Select-Object Name) -match ‘ATLDC’

Or using what you submitted a couple of ways…

$aDCs = Get-ADDomainController -Filter * | Select-Object Name

ForEach ($sDC in $aDCs) {
If ($sDC.Name -like “ATL*”) {

Note - if you are just outputting to the console, you really don’t need the write-* thing.
Secondly, if you are to put things on the pipeline, Write-Host is not a thing to use.

You can write to the console without write-*, just by using strings as follows…

Write-Output ‘whatever’

even in output combination.

‘Getting DC named’ + $sDC.Name
“Getting DC named $sDC.Name”

You only really need Write-Host, if you are outputting color to the console in your output, or other formatting needs.

Thanks, that worked. In all I tried I didn’t get the syntax correct. And yes, the output to the console is due to it being part of a larger script - actually reading the STDOut with -ForegroundColor parameter and pulling that into a custom GUI. Thanks again for the push in the right direction.