<p>The issue is that you can't reference the changed value in the same task.</p>
<p>I created a new Powershell task and just called <code>Write-Host &quot;env:IP environment variable set to ($env:IP)&quot;</code> which output the IP address correctly.</p>

CC BY-SA 4.0
2021-03-27T15:13:53.560Z
2021-03-27T15:13:53.560Z
cdsln
<p>Pipeline Variables are handed to PowerShell as environment variables, which means that <code>$ip</code> exists only with the script while you want to use <code>$env:ip</code> to pick the environment variable value.</p>
<p><strong>Note</strong> Secret variables are <strong>not</strong> passed automatically: you must add an explicit <code>env:</code> in YAML or as arguments in Classic Editor. The latter means that the PowerShell code must have a <code>param</code> declaration (e.g. <code>param($ip)</code>)and the Task use <em>Arguments</em> field (e.g. <code>-ip $(ip)</code>).</p>
<p><img src="https://docs.microsoft.com/en-us/azure/devops/pipelines/media/get-started-designer/powershell-task-2.png?view=azure-devops" alt="" /></p>

CC BY-SA 4.0
2021-03-27T11:22:12.807Z
2021-03-27T11:22:12.807Z
Giulio Vian
<p>I've tried to figure this out and I can't see why my ipaddress is not getting set.</p>
<p>I've checked code samples and they all suggest I am doing everything correct.</p>
<p><code>ip</code> variable defined here:</p>
<p><a href="https://i.sstatic.net/0y5Jr.png" rel="noreferrer"><img src="https://i.sstatic.net/0y5Jr.png" alt="enter image description here" /></a></p>
<p>This is my powershell script:</p>
<pre><code># Write your PowerShell commands here.

Write-Host &quot;AGENTIPADDRESS variable is set to ($env:AGENTIPADDRESS)&quot;

$ipaddress = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
Write-Host &quot;ipaddress variable is set to ($ipaddress)&quot;
Write-Host &quot;ip pipeline variable to ($ip)&quot;

Write-Host &quot;##vso[task.setvariable variable=IP;]$ipaddress&quot;
Write-Host &quot;##vso[task.setvariable variable=env:IP;]$ipaddress&quot;
Write-Host &quot;##vso[task.setvariable variable=$env:IP;]$ipaddress&quot;
echo &quot;##vso[task.setvariable variable=IP;]$ipaddress&quot;
Write-Host &quot;##vso[task.setvariable variable=IP;]333&quot;
Write-Host &quot;##vso[task.setvariable variable=IP;]$ipaddress&quot;
echo &quot;##vso[task.setvariable variable=IP]999&quot;
echo &quot;##vso[task.setvariable variable=IP;]888&quot;

Write-Host &quot;##vso[task.setvariable variable=IP;]$ipaddress&quot;
Write-Host &quot;##vso[task.setvariable variable=IP;]$ipaddress&quot;
Write-Host &quot;##vso[task.setvariable variable=IP]114&quot;
Write-Host &quot;##vso[task.setvariable variable=IP;]123&quot;

Write-Host &quot;ip environment variable to ($ip)&quot;
Write-Host &quot;env:IP environment variable to ($env:IP)&quot;

Write-Host &quot;Set environment variable to ($env:AGENTIPADDRESS)&quot;
</code></pre>
<p>And here is my output:</p>
<p><a href="https://i.sstatic.net/zXkfe.png" rel="noreferrer"><img src="https://i.sstatic.net/zXkfe.png" alt="enter image description here" /></a></p>
<p>Why can't I set &amp; read the <code>ip</code> variable?</p>

<azure-devops><azure-pipelines><environment-variables><powershell>
Azure Devops Pipeline variables in Powershell
cdsln
# Azure Devops Pipeline variables in Powershell

(Asked by: username_0 on 2021-03-26)

I've tried to figure this out and I can't see why my ipaddress is not getting set.
I've checked code samples and they all suggest I am doing everything correct.
ip variable defined here:

This is my powershell script:
# Write your PowerShell commands here.

Write-Host "AGENTIPADDRESS variable is set to ($env:AGENTIPADDRESS)"

$ipaddress = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
Write-Host "ipaddress variable is set to ($ipaddress)"
Write-Host "ip pipeline variable to ($ip)"

Write-Host "##vso[task.setvariable variable=IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=env:IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=$env:IP;]$ipaddress"
echo "##vso[task.setvariable variable=IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=IP;]333"
Write-Host "##vso[task.setvariable variable=IP;]$ipaddress"
echo "##vso[task.setvariable variable=IP]999"
echo "##vso[task.setvariable variable=IP;]888"

Write-Host "##vso[task.setvariable variable=IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=IP]114"
Write-Host "##vso[task.setvariable variable=IP;]123"

Write-Host "ip environment variable to ($ip)"
Write-Host "env:IP environment variable to ($env:IP)"

Write-Host "Set environment variable to ($env:AGENTIPADDRESS)"

And here is my output:

Why can't I set & read the ip variable?

**Tags:** azure-devops, azure-pipelines, environment-variables, powershell


---

## 2 answers

### Answer by username_0 on 2021-03-27. Score: 2 | Accepted answer

The issue is that you can't reference the changed value in the same task.

I created a new Powershell task and just called `Write-Host "env:IP environment variable set to ($env:IP)"` which output the IP address correctly.


---

### Answer by username_1 on 2021-03-27. Score: 3

Pipeline Variables are handed to PowerShell as environment variables, which means that `$ip` exists only with the script while you want to use `$env:ip` to pick the environment variable value.

**Note** Secret variables are **not** passed automatically: you must add an explicit `env:` in YAML or as arguments in Classic Editor. The latter means that the PowerShell code must have a `param` declaration (e.g. `param($ip)`)and the Task use *Arguments* field (e.g. `-ip $(ip)`).

![](https://docs.microsoft.com/en-us/azure/devops/pipelines/media/get-started-designer/powershell-task-2.png?view=azure-devops)


---

