FQDN of a Windows Server in Domain using batch / cmd script

Quick one for you (and there’s probably easier ways of doing this)…only took ten minutes to work it out tho.

I needed to get the Fully Qualified Domain Name of a computer on the network into an environment variable in a batch script.

Here’s the code snippet:

1@ECHO OFF
2
3FOR /f "tokens=2,* delims= " %%a in ('IPCONFIG ^/ALL ^| FINDSTR "Primary Dns"') do set tempsuffix=%%b
4FOR /f "tokens=1,2 delims=:" %%a in ('echo %tempsuffix%') do set dnssuffix=%%b
5SET FQDN=%COMPUTERNAME%.%DNSSUFFIX:~1%
6
7ECHO Server FQDN: %FQDN%