You can use the Set-Variable cmdlet. Passing $global:var3 sends the value of $var3, which is not what you want. You want to send the name.

$global:var1 = $null

function foo (b, $varName)
{
   Set-Variable -Name $varName -Value (b) -Scope Global
}

foo 1 2 var1

This is not very good programming practice, though. Below would be much more straightforward, and less likely to introduce bugs later:

$global:var1 = $null

function ComputeNewValue (b)
{
   b
}

$global:var1 = ComputeNewValue 1 2
Answer from latkin on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.utility › set-variable
Set-Variable (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn
Set-Variable -Name "desc" -Value "A description" Get-Variable -Name "desc" ... This example creates a global, read-only variable that contains all processes on the system, and then it displays all properties of the variable.
🌐
Netwrix
netwrix.com › en › resources › blog › powershell-variables-and-arrays
How to Use PowerShell Variables
August 26, 2025 - PowerShell variables have a Description property that you can set using the Set-Variable cmdlet with the Description parameter: Set-Variable -Name "myVariable" -Description "This is my sample variable" To exclude certain variables when you are ...
Discussions

Setting a global PowerShell variable from a function where the global variable name is a variable passed to the function - Stack Overflow
I couldn't make even the 3rd example to work - still not assigning via command line (works on VS and ISE) 2017-02-20T10:35:17.29Z+00:00 ... I am seriously so annoyed by this powershell weirdness! Seems to me that declaring a variable in a top level (not in function) should make it global and ... More on stackoverflow.com
🌐 stackoverflow.com
azure devops - How does one set a variable in PowerShell? - Stack Overflow
I am trying to set variables in a PowerShell, so that I can use them in custom conditions in release definitions, so that I can prevent phases from running when a VSTS variable is set to $False or 0. More on stackoverflow.com
🌐 stackoverflow.com
Permanent Variable Set-Up In Powershell
70+, gawd what's in there and are you sure you don't want to just define default parameter values instead More on reddit.com
🌐 r/PowerShell
8
6
November 30, 2022
Set-content how write $variable as it is and not as variable
Read this: about_Quoting_Rules . More on reddit.com
🌐 r/PowerShell
9
6
July 8, 2022
🌐
SS64
ss64.com › ps › set-variable.html
Set-Variable - PowerShell - SS64.com
In the example above, the value is enclosed in parentheses this executes the command: (Get-Process) before storing the result in the variable, rather than just storing the text "Get-Process". “O, swear not by the moon, the fickle moon, the inconstant moon, that monthly changes in her circle orb, Lest that thy love prove likewise variable” ~ Shakespeare · Clear-Variable - Remove the value from a variable. Get-Variable - Get a PowerShell ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.utility › new-variable
New-Variable (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn
At line:1 char:1 + New-Variable ... New-Variable to protect a variable from being overwritten. The first command creates a new variable named Max and sets its value to 256....
🌐
Codecademy
codecademy.com › docs › powershell › variables
PowerShell | Variables | Codecademy
June 8, 2023 - Variables in PowerShell are referenced using a dollar sign $ followed by a valid variable name.
Find elsewhere
🌐
Varonis
varonis.com › blog › powershell-variable-scope
PowerShell Variable Scope Guide: Using Scope in Scripts and Modules
October 13, 2022 - Initially, the value of $greeting in the global console scope is empty or null, and the scopetest.ps1 file sets the value of $greeting. By using dot source notation, the script brings the variable value in the parent scope. You can also use the call operator/ampersand (&) to run a script or function, and its scope will not be added to the current scope. Understanding scoping in PowerShell is essential when writing scripts, modules, and functions.
🌐
LazyAdmin
lazyadmin.nl › home › how to use powershell variables
How to use PowerShell Variables — LazyAdmin
January 18, 2024 - It’s also possible to create a variable in PowerShell without specifying any value, even not $null. For this, you will have to use the cmdlet Set-Variable, which creates the variable with the value $null for you. ... The set-variable cmdlet has some advantages when creating a variable, you could for example give a description to your variable or make it read online.
🌐
EITCA Academy
eitca.org › home › what is the syntax for creating a variable and prompting the user for input in powershell?
What is the syntax for creating a variable and prompting the user for input in PowerShell? - EITCA Academy
August 5, 2023 - This cmdlet displays a prompt and ... name and store it in the "myVariable" variable, you would use the following syntax: powershell $myVariable = Read-Host "Please enter your name"...
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › powershell tutorials › set variables in powershell: a quick guide
Set Variables in PowerShell: A Quick Guide - SharePoint Diary
October 9, 2025 - The easiest way to set a variable ... name and then the assignment operator (=). For example, to assign the value “123” to a variable named “myVar”, you would use the following command:...
🌐
O'Reilly
oreilly.com › library › view › mastering-windows-powershell › 9781787126305 › 4f64bdbd-c317-4ac5-912c-66c77ed455ae.xhtml
Set-Variable - Mastering Windows PowerShell Scripting - Second Edition [Book]
October 27, 2017 - Set-Variable Set-Variable allows you to change the value and certain aspects of the created variable. For example, this sets the value of an existing variable: $objectCount = 23... - Selection from Mastering Windows PowerShell Scripting - Second ...
Authors   Chris DentBrenton J.W. Blawat
Published   2017
Pages   440
🌐
PDQ
pdq.com › powershell › new-variable
New-Variable - PowerShell Command | PDQ
This example shows how to use the ReadOnly option of New-Variable to protect a variable from being overwritten. The first command creates a new variable named Max and sets its value to 256.
🌐
Opensource.com
opensource.com › article › 19 › 8 › variables-powershell
Variables in PowerShell | Opensource.com
August 29, 2019 - In PowerShell, you create a variable by defining a variable name and then setting its value with the Set-Variable command. The example below creates a new variable called FOO and sets its value to the string $HOME/Documents:
🌐
Composer
getcomposer.org › doc › 00-intro.md
Introduction - Composer
Using PowerShell: PS C:\bin> Set-Content composer.bat '@php "%~dp0composer.phar" %*' Add the directory to your PATH environment variable if it isn't already. For information on changing your PATH variable, please see this article and/or use your search engine of choice.
🌐
Android Developers
developer.android.com › android studio › command-line tools
Command-line tools | Android Studio | Android Developers
The steps vary depending on your OS, but read How to set environment variables for general guidance.
🌐
Python documentation
docs.python.org › 3 › library › venv.html
venv — Creation of virtual environments
On Microsoft Windows, it may be required to enable the Activate.ps1 script by setting the execution policy for the user. You can do this by issuing the following PowerShell command:
🌐
Claude
code.claude.com › docs › en › env-vars
Environment variables - Claude Code Docs
1 month ago - Claude Code supports the following environment variables to control its behavior. Set them in your shell before launching claude, or configure them in settings.json under the env key to apply them to every session or roll them out across your team.
🌐
GitLab
docs.gitlab.com › ci › variables
CI/CD variables | GitLab Docs
If variable expansion is enabled, the only non-alphanumeric characters you can use in the variable value are: _, :, @, -, +, ., ~, =, /, and ~. When the setting is disabled, all characters can be used. Select Update variable. ... Be a single line with no spaces. Be 8 characters or longer. Not match the name of an existing predefined or custom CI/CD variable. If a process outputs the value in a slightly modified way, the value can’t be masked. For example, if the shell adds \ to escape special characters, the value isn’t masked:
Top answer
1 of 5
8

Setting variables in PowerShell itself is trivial, merely assign (= operator) to the variable:

$VarName = 42

But likely the question is looking for a way to set Environment variables which the (VSTS) application can 'see' and 'use' when it is called from PowerShell.

To assign to any session Environment variable prefix the name with "ENV" so it looks like this:

$Env:VarName = 4201

This will remain for the current PowerShell session or life of the console -- other sessions in other windows or run later will not see or be affected by such settings.

If you wish to have a persistent Environment variable then you must set that in the registry, either for the User (HKey_Local_User) or Computer (Hkey_Local_Machine).

These will get the environment settings from the registry (maybe be different that currently set in your process):

[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::Machine)
[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::User)

And the Set commands are similar but include the new value (e.g., variable 'Tools' is set to 'C:\':

[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::User)

There is also a "location" enumeration for "Process" (instead of User or Machine) but it is simpler to set this using the PowerShell $Env:VariableName shown above.

    [Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Process)
2 of 5
7

You need to use Write-Host with a special string format:

Write-Host "##vso[task.setvariable variable=YourVSTSVariableName]$yourPowershellVariable"