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.

2
  • 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. Commented Feb 17, 2018 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. Commented Feb 19, 2018 at 16:46

2 Answers 2

11
for /f "tokens=*" %%i in ('powershell /command "[System.Net.Dns]::GetHostByName((hostname)).HostName"') do set return=%%i
echo %return%
Sign up to request clarification or add additional context in comments.

Comments

3
+25

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%'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.