Generally, it would be better to pass info to the script via a parameter rather than a global (environment) variable. But if that is what you need to do you can do it this way:

$env:FOO = 'BAR'; ./myscript

The environment variable $env:FOO can be deleted later like so:

Remove-Item Env:\FOO
Answer from Keith Hill 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
The Set-Variable cmdlet assigns a value to a specified variable or changes the current value. If the variable does not exist, the cmdlet creates it. These commands set the value of the $desc variable to A description, and then gets the value ...
Discussions

Setting local variables in Powershell/CMD
in cmd c:\>set var1 = 777 reference it c:\>echo %var1% In PowerShell PS c:\> $var1 = 777 reference it PS c:\> Write-Host $var1 More on reddit.com
🌐 r/sysadmin
6
0
February 16, 2022
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
Powershell Script to run a basic set of commands
I see you cross posted. See my answer for you here: https://stackoverflow.com/questions/56406757/how-to-create-a-basic-powershell-script-to-run-simple-commands-output-results More on reddit.com
🌐 r/PowerShell
17
3
June 1, 2019
🌐
ITPro Today
itprotoday.com › home › powershell
PowerShell How-To: Emulating Cmd.exe's Set Command
June 4, 2024 - To get this behavior, my PowerShell Set function uses the MyInvocation object's InvocationName and Line properties to capture information about the function's command line. The InvocationName property contains the name of the command. The Line property returns the line used to invoke the command.
🌐
PDQ
pdq.com › powershell › set-item
Set-Item - PowerShell Command | PDQ
Learn how to use the Microsoft PowerShell command Set-Item. PDQ breaks down uses of Set-Item with parameters and helpful examples.
🌐
Reddit
reddit.com › r/sysadmin › setting local variables in powershell/cmd
r/sysadmin on Reddit: Setting local variables in Powershell/CMD
February 16, 2022 -

Trying to get a quick script that would determine if a specific system process is running.

I am running into an issue where I cannot set a local variable.

C:\WINDOWS\system32>var1 = 777

'var1' is not recognized as an internal or external command, operable program or batch file.

This seems to be an issue with more than one of our endpoints. Is there a way to fix this issue using the terminal?

🌐
GitHub
github.com › MicrosoftDocs › PowerShell-Docs › blob › main › reference › 7.5 › Microsoft.PowerShell.Utility › Set-Variable.md
PowerShell-Docs/reference/7.5/Microsoft.PowerShell.Utility/Set-Variable.md at main · MicrosoftDocs/PowerShell-Docs
Set-Variable -Name "processes" -Value (Get-Process) -Option Constant -Scope Global -Description "All processes" -PassThru | Format-List -Property * The command uses the Set-Variable cmdlet to create the variable.
Author   MicrosoftDocs
Find elsewhere
🌐
Windows 10 Forums
tenforums.com › general-support › 217307-some-help-powershell-what-does-set-command-do.html
Some help with powershell what does the set command do? - Windows 10 Help Forums
December 24, 2024 - I want to access the path variable in powershell and I accidentally used the set command. It asks me to supply the following paramters. Name[0], Name[1].. etc. What does it do?
🌐
PDQ
pdq.com › powershell › set-variable
Set-Variable - PowerShell Command | PDQ
PS C:\> Set-Variable -Name "processes" ... -PassThru | Format-List -Property * This command creates a global, read-only variable that contains all processes on the system, and then it displays all properties of the variable....
🌐
GitHub
github.com › MicrosoftDocs › PowerShell-Docs › blob › main › reference › 7.4 › Microsoft.PowerShell.Utility › Set-Variable.md
PowerShell-Docs/reference/7.4/Microsoft.PowerShell.Utility/Set-Variable.md at main · MicrosoftDocs/PowerShell-Docs
Set-Variable -Name "processes" -Value (Get-Process) -Option Constant -Scope Global -Description "All processes" -PassThru | Format-List -Property * The command uses the Set-Variable cmdlet to create the variable.
Author   MicrosoftDocs
🌐
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 in PowerShell is to use the assignment operator: In PowerShell, variables are created and assigned values using the “$” symbol followed by the variable name and then the assignment operator (=). For example, ...
🌐
TechTarget
techtarget.com › searchwindowsserver › tip › Top-PowerShell-commands-you-must-know-with-cheat-sheet
Top PowerShell commands you must know, with cheat sheet | TechTarget
Use Get-Command to see the cmdlets in a specific PowerShell module. The list is much longer than the screenshot. To find all the cmdlets related to updating users or resources, use a wildcard to find all the Set-* commands.
🌐
Reddit
reddit.com › r/powershell › permanent variable set-up in powershell
r/PowerShell on Reddit: Permanent Variable Set-Up In Powershell
November 30, 2022 -

I have 70+ permanent variables I want to setup in Powershell. I read Microsoft Docs and used Set-Item command for setting 2 permanent variables. Problem is I am not able to use it.

Like --

PS> Set-Item -Path Env:\hp_prod -Value 'hp/hello123@database1.dc1:1521/dc1.internal'

This is example of my database connection string. So, I want to use it like this to connect database:

PS> sqlplus $hp_prod

In Linux environment if I put this in "bash_profile" file then it works there. So, I want to do same in Powershell.

Kindly advice.

🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.security › set-executionpolicy
Set-ExecutionPolicy (Microsoft.PowerShell.Security) - PowerShell | Microsoft Learn
At line:1 char:20 + Set-ExecutionPolicy ... ExecutionPolicy : Unrestricted · The Set-ExecutionPolicy cmdlet uses the ExecutionPolicy parameter to specify the Restricted policy....
🌐
Reddit
reddit.com › r/powershell › powershell script to run a basic set of commands
r/PowerShell on Reddit: Powershell Script to run a basic set of commands
June 1, 2019 -

Hi guys,

Generally I associate myself with the Linux world and I'm more used to the bash shell, but with a recent change of career I find myself administering Windows Servers a lot more.

I want to try and make my life as simple as possible and make even simple commands be reduced to 1 click to run the script or automated all together. As such, I'm a total PowerShell noob.

I want to make a script which will change directory (Set-Location), findstr, then display the time and date (Get-Date) all in an small dialog box or even the shell itself.

Can someone direct me to resources to learn about this by any chance?

🌐
PDQ
pdq.com › powershell › set-content
Set-Content - PowerShell Command | PDQ
Whereas the Add-Content cmdlet appends content to a file, Set-Content replaces the existing content. You can type the content in the command or send content through the pipeline to Set-Content .In file system drives, the Set-Content cmdlet overwrites or replaces the content of one or more files ...