21

How would I find the name of the PC running my batch program on it?

I would like to find the name of a PC that is running my batch program and be able to store it as a variable. Any help?

3
  • 4
    you can retrieve the hostname of a system from the cmd or powershell command line with hostname. you shouldn't need to store it as a var, just invoke it inline as you need it. Dec 14, 2014 at 9:44
  • @FrankThomas - Why don't you provide this as (perhaps with a short example) an answer instead?
    – Nifle
    Dec 14, 2014 at 9:51
  • 2
    hostname is an exe, and while it outputs the machine name on console, it will be non-trivial to capture it in a batch file. Luckily, there is already an environment variable set, and available (ComputerName) to use on typical windows system. Dec 14, 2014 at 11:20

3 Answers 3

10

As Vikas Gupta has answered, you can use the pre-defined %COMPUTERNAME% environment variable that already contains the computer name. From a practical stand point, this should be all you need.

However, it is possible for a batch file to over-write the value, so it is not guaranteed that the value be correct.

You can use WMIC to directly read the computer name.

for /f "skip=1 delims=" %%A in (
  'wmic computersystem get name'
) do for /f "delims=" %%B in ("%%A") do set "compName=%%A"

The extra FOR loop eliminates unwanted carriage return characters that are an artifact of FOR /F interacting with the Unicode output of WMIC. With only one loop there is a carriage return at the end of each line that can cause problems.

2
  • 2
    is there a reason for using wmic instead of just calling hostname?
    – eis
    Dec 14, 2014 at 16:58
  • I thought of that after I wrote the answer. It certainly would be simpler. I'm no expert on these issues, but based on serverfault.com/q/260563/201695, I think host name and computer name are not guaranteed to be the same. But I think they are supposed to be the same under normal circumstances.
    – dbenham
    Dec 14, 2014 at 17:33
38

On Windows, typically an environment variable is already set and available for you to use -

echo %ComputerName%
5
  • 1
    Which versions of Windows does this apply to? (Worked for me in Windows 7.) Dec 14, 2014 at 11:40
  • 1
    This variable is present at least all the way back to Windows NT. Dec 14, 2014 at 18:15
  • interestingly enough though, it won't work in a powershell; only in cmd.exe. odd that. Dec 14, 2014 at 21:45
  • Powershell has its own way of representing special things like Environment Variables... following works - Get-Item Env:\ComputerName Dec 14, 2014 at 21:55
  • 2
    @VikasGupta You could just use $env:computername (case-insensitive too).
    – Bob
    Dec 14, 2014 at 22:35
0

In PowerShell you can also use:

[Environment]::MachineName

Here the value comes from .Net so it avoids the issue of using $Env:ComputerName.

1
  • What's the issue of using $Env:ComputerName?
    – Shadow
    Feb 11, 2019 at 23:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .