Skip to content

Get-DnsClientServer

Windows: Retrieves DNS client and server configuration

#Requires -Version 5.1
#Requires -Modules DnsClient

[CmdletBinding()]
Param
(
    [string]$ComputerName = $env:COMPUTERNAME,

    [string]$InterfaceAlias,

    [pscredential]$Credential
)

Process
{
    try
    {
        $session = $null
        $cimParams = @{
            'ErrorAction' = 'Stop'
        }

        if ($ComputerName -ne $env:COMPUTERNAME)
        {
            $sessionParams = @{
                'ComputerName' = $ComputerName
            }
            if ($null -ne $Credential)
            {
                $sessionParams.Add('Credential', $Credential)
            }
            $session = New-CimSession @sessionParams
            $cimParams.Add('CimSession', $session)
        }

        $interfaces = Get-DnsClient @cimParams
        if (-not [string]::IsNullOrWhiteSpace($InterfaceAlias))
        {
            $interfaces = $interfaces | Where-Object { $_.InterfaceAlias -like $InterfaceAlias }
        }

        $results = foreach ($interface in $interfaces)
        {
            $serverAddresses = Get-DnsClientServerAddress -InterfaceIndex $interface.InterfaceIndex @cimParams
            
            [PSCustomObject]@{
                InterfaceAlias = $interface.InterfaceAlias
                InterfaceIndex = $interface.InterfaceIndex
                DNSServers     = ($serverAddresses.ServerAddresses) -join ', '
                RegisterSuffix = $interface.RegisterThisConnectionsAddress
                SuffixSearch   = ($interface.ConnectionSpecificSuffix)
                ComputerName   = $ComputerName
            }
        }

        Write-Output $results
    }
    catch
    {
        throw
    }
    finally
    {
        if ($null -ne $session)
        {
            Remove-CimSession $session
        }
    }
}

Specifies the name of the computer to query. Defaults to the local computer.

Specifies the alias of a specific network interface to query.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.