I have a script that uses invoke-request to get a remote ID string. How can I retrieve the value of instanceID and set it to (SET instanceId) within .bat script

powershell $instanceId =(Invoke-WebRequest -Uri exit


  • The variable isn't set in PoSh - but simply output.
  • The batch wraps the powershell command in a parsing for /f loop and sets a batch variable.

@Echo off
For /f "usebackqdelims=" %%A in (
  `Powershell.exe -nologo -NoProfile -command "(Invoke-WebRequest -Uri"`
) Do Set InstanceID=%%A
Echo InstanceID=%InstanceID%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.