10

What I am trying to do is to save the output of a powershell command (run from a batch script) and use it in the batch script.

Can you please advise me what to do?

The power shell comand is:

[System.Net.Dns]::GetHostByName((hostname)).HostName

I want to use the output in the batch script.

P.S.

It will be even better if I can get the full computer name/hostname/fully qualified domain name (FQDN) from cmd and not from powershell. But the full computer name is not the concatenation of the ComputerName and the UserDNSDomain variables.

Community
  • 1
  • 1
fanciulla
  • 175
  • 1
  • 5
  • 18
  • 1
    From powershell, '$env:computername' can get you the computer name if that's what you mean by your PS. @thufir I see the bounty is asking for a 100% powershell solution, but I don't see the batch code looking to be converted. – Sambardo Feb 17 '18 at 20:58
  • @thufir: I don't understand. The question is how to use the 100% powershell command from batch, so the question itself should be the answer, you are looking for. If it's not, please clarify or even better ask a new question. – Stephan Feb 19 '18 at 16:46

2 Answers2

11
for /f "tokens=*" %%i in ('powershell /command "[System.Net.Dns]::GetHostByName((hostname)).HostName"') do set return=%%i
echo %return%
Stephan
  • 53,940
  • 10
  • 58
  • 91
3

You can do this in batch using nslookup which does the same DNS-search:

for /f "tokens=1*" %%a in ('nslookup hostname ^| findstr /i "name"') do set return=%%b
echo Hello '%return%'
Frode F.
  • 52,376
  • 9
  • 98
  • 114