Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign output of powershell command to variable

I am struggling to be able to run a command and attach to variable in windows!

On mac I would run:

export TOKEN="$(curl --header "Metadata-Flavor: Google" --get --data-urlencode "audience=http://vault/${ROLE}" --data-urlencode "format=full" "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity")"

But struggling to run same on windows (have tried piping output using > but no luck)!

What am I obviously missing!

like image 427
Andrew Avatar asked Oct 12 '25 10:10

Andrew


1 Answers

You can assign the output of any command in PowerShell by just using this syntax.

$myNewVar = hostname #or any other command

What will happen is that the output of the command is consumed and stored in the variable, so nothing gets output to the screen.

if you want to assign a variable while also outputting to the screen, us the Tee command.

hostname | tee-object -variable myNewVar
like image 163
FoxDeploy Avatar answered Oct 15 '25 14:10

FoxDeploy