title: Azure DevOps - Setting and Using Variables in PowerShell Scripts
body: <p>I have an Azure DevOps build pipeline that has two separate PowerShell scripts. In the first script, I am getting a value from an XML file and setting that value in an environment variable. In my second script, I want to use the value in the environment variable. Unfortunately, I don't see the environment variable getting set. At this time, I have:</p>
  
  <p><strong>Script 1:</strong></p>
  
  <pre><code>$myXml = [xml](Get-Content ./MyXml.xml)  
  $departmentId = $myXml.Department.Id
  
  Write-Host ##vso[task.setvariable variable=DepartmentId;]$departmentId    
  Write-Host "Set environment variable to ($env:DepartmentId)"
  
  Get-ChildItem Env:
  
  Write-Host "Department Id ($departmentId)"
  </code></pre>
  
  <p>When script 1 runs, I see:</p>
  
  <pre><code>Set environment variable to ()
  [All of the environment variable BUT, I DO NOT SEE ONE NAMED "DepartmentId"]
  Department Id (1)
  </code></pre>
  
  <p>Notice: 1) The <code>$env:DepartmentId</code> value is not printing in the "Set environment variable" statement and 2) The <code>DepartmentId</code> value is NOT listed in the environment variable list. My intention is to use <code>DepartmentId</code> in the second script, which looks like this:</p>
  
  <p><strong>Script 2:</strong></p>
  
  <pre><code>Write-Host "Using Department: $(env:DepartmentId)"
  </code></pre>
  
  <p>At this time, the script just shows:</p>
  
  <pre><code>env:DepartmentId : The term 'env:DepartmentId' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  </code></pre>
  
  <p>I've seen the other related SO questions and reviewed the docs. However, this simply isn't working. I don't understand what I'm doing wrong. Can someone please show me how to fix this and explain what I'm doing wrong? Thank you!</p>