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 OverflowGenerally, 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
Making a 'subshell' by invoking powershell with a script block allows you to scope the changes to the environment:
pwsh -Command { $env:MYVAR="myvalue"; .\path\to.exe }
Setting local variables in Powershell/CMD
Permanent Variable Set-Up In Powershell
Powershell Script to run a basic set of commands
Videos
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?
To do it without a temp file you can do:
@ECHO off
FOR /F %%i in ('powershell.exe -noprofile "(get-date).DayOfWeek"') DO SET dow=%%i
ECHO Day of week %dow%
PAUSE
from the command prompt remove the double %%:
FOR /F %i in ('powershell.exe -noprofile "(get-date).DayOfWeek"') DO SET dow=%i
Using -noprofile will make it run faster if you have a profile set up for PowerShell.
powershell -noprofile -command {[environment]::SetEnvironmentVariable("dow", (get-date).DayofWeek, "User")}
Obviously you need to open other cmd to see the change.
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.
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?
