How to find the PCs name in a batch script?



As an IT administrator, it is important to be able to identify the computer name of a given PC in a batch script. This can be done using a Windows command line utility, hostname.exe.

The hostname.exe utility is a Windows system utility that is included in all versions of Windows since Windows XP. It is used to display the name of the computer or to set a new name for the computer. The syntax of the command is as follows:

hostname [-s] [-a] [-i] [-d] [-f]

The -s option displays only the short name of the computer, the -a option displays the fully qualified domain name, the -i option displays the IP address, and the -d option displays the domain name. The -f option displays the full name of the computer.

To use the hostname.exe utility in a batch script, the following command can be used:

hostname > %COMPUTERNAME%

This command will output the name of the computer to the %COMPUTERNAME% environment variable. This environment variable can then be used in the batch script to identify the name of the computer.

For example, the following batch script will output the name of the computer to a file:

@echo off
hostname > %COMPUTERNAME%
echo %COMPUTERNAME% > \C:\\ComputerName.txt\

This batch script will output the name of the computer to the %COMPUTERNAME% environment variable, and then output the value of the %COMPUTERNAME% environment variable to a text file located in the root of the C drive.

Using the hostname.exe utility in a batch script is a great way to quickly and easily identify the name of the computer. It is a quick and easy way to get the name of the computer without having to manually look it up.

Inquire Now

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Find your next full or part-time role here

ScaleDesk can either help you find your next full time gig or a part time side gig where you can create extra income!

Onboard with us once
Skip HR screening and go to the final interview with with only your resume and a video interview you never have to redo
Get paid electronically every month for the hours you work
We will be your reference even if you work for us once

IT Teams: Use ScaleDesk to augment your team

Schedule Demo