How to Get the Computer Name in a Batch Script
Knowing the name of the computer a script is running on is essential for many automation tasks. You might need it to create uniquely named log files in a central location, to customize a path for a network resource, or simply to report which machine a particular task was run on. Fortunately, Windows provides a simple, built-in environment variable for this exact purpose.
This guide will teach you the standard and most direct way to get the computer's name using the %COMPUTERNAME% variable. We will also cover the hostname command as an alternative and show a practical example of how to use this information to create a machine-specific log file.
The Core Method: The %COMPUTERNAME% Variable
The simplest, fastest, and most common way to get the computer name is by using the built-in %COMPUTERNAME% environment variable. This variable is set by the system during startup and is always available in any command prompt session.
Because it's a variable, you can use it directly in your commands without needing to run any external programs. This makes it the most efficient and highly recommended method.
The Alternative Method: The hostname Command
Windows also includes a small command-line utility called hostname.exe whose only job is to print the computer's name to the console.
Command: hostname
While this command works perfectly, it is slightly less efficient for scripting because it requires running an external executable. To use its output in a script, you must capture it with a FOR /F loop, which is an extra step that using %COMPUTERNAME% avoids.
Basic Example: Displaying the Computer Name
This script demonstrates both methods for getting the computer name.
@ECHO OFF
ECHO --- Getting the Computer Name ---
ECHO.
ECHO Method 1 (Recommended): Using the %%COMPUTERNAME%% variable
ECHO The computer name is: %COMPUTERNAME%
ECHO.
ECHO Method 2: Using the 'hostname' command
hostname
In the output you see that both methods produce the exact same result.
--- Getting the Computer Name ---
Method 1 (Recommended): Using the %%COMPUTERNAME%% variable
The computer name is: MY-DESKTOP-PC
Method 2: Using the 'hostname' command
MY-DESKTOP-PC
Storing the Name in a Variable
In a script, you'll often want to store the name in another variable for clarity or to build a string.
- With
%COMPUTERNAME%: This is a simple, direct assignment. - With
hostname: This requires aFOR /Floop to capture the command's output.
@ECHO OFF
SETLOCAL
REM --- Method 1: Direct assignment (Recommended) ---
SET "MachineName_Var=%COMPUTERNAME%"
ECHO From variable: %MachineName_Var%
REM --- Method 2: Capturing command output ---
SET "MachineName_Cmd="
FOR /F %%N IN ('hostname') DO (
SET "MachineName_Cmd=%%N"
)
ECHO From command: %MachineName_Cmd%
ENDLOCAL
As you can see, using the built-in variable is much more straightforward.
Common Pitfalls and How to Solve Them
Getting the computer name is a very reliable operation with few pitfalls. The main consideration is how the name is used.
- Using the Name in a Path: A computer name is a simple string and should not contain spaces or special characters that are illegal in file paths. It is generally safe to use directly. However, it's still a good practice to quote the full path you construct with it, just in case.
REM Good practice to quote the whole path.
SET "LogFile=\\LogServer\Logs\%COMPUTERNAME%.log" - Remote Desktop Sessions: In a Remote Desktop or Terminal Services session, you might also see variables like
%CLIENTNAME%(the name of the computer connecting to the server) and%SESSIONNAME%(the name of the terminal session). For getting the name of the machine the script is running on,%COMPUTERNAME%is always the correct choice.
Practical Example: Creating a Machine-Specific Log File
This is a classic use case. The script performs some action and then writes a log file to a central network share. By including the computer name in the log's filename, you can easily tell which machine the log came from.
@ECHO OFF
SETLOCAL
REM --- Configuration ---
SET "LogShare=\\FileServer\CentralLogs"
SET "LogTimestamp=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%"
REM --- Build the log filename using the computer name ---
SET "LogFileName=%COMPUTERNAME%_Status_%LogTimestamp%.log"
SET "FullLogPath=%LogShare%\%LogFileName%"
ECHO --- System Health Check ---
ECHO.
ECHO This script will save its results to:
ECHO "%FullLogPath%"
ECHO.
REM --- Redirect the output of a block of commands to the log file ---
(
ECHO Log for machine: %COMPUTERNAME%
ECHO Report generated on: %DATE% %TIME%
ECHO.
ECHO --- Disk Space ---
WMIC LOGICALDISK WHERE "DeviceID='C:'" GET FreeSpace,Size /VALUE
ECHO.
ECHO --- Running Processes ---
tasklist
) > "%FullLogPath%"
ECHO [SUCCESS] Log file created.
ENDLOCAL
Conclusion
Getting the computer's name is a simple but essential task for creating adaptable and traceable scripts.
- The
%COMPUTERNAME%environment variable is the standard, most efficient, and highly recommended method. It is instantly available and requires no external commands. - The
hostnamecommand provides the same information but is less direct for scripting as it requires aFOR /Floop to capture its output.
For its simplicity and performance, you should always default to using the %COMPUTERNAME% variable in your batch scripts.