Copyget-content test.env | foreach {
$name, $value = $_.split('=')
set-content env:\$name $value
}
assuming you mean "set one environment variable per line in the file".
Answer from TessellatingHeckler on Stack OverflowCopyget-content test.env | foreach {
$name, $value = $_.split('=')
set-content env:\$name $value
}
assuming you mean "set one environment variable per line in the file".
Polished version of @TessellatingHeckler's for future reference.
CopyGet-Content .env | foreach {
$name, $value = $_.split('=')
if ([string]::IsNullOrWhiteSpace($name) -or $name.Contains('#')) {
# skip empty or comment line in ENV file
return
}
Set-Content env:\$name $value
}
How to invoke one PS1 from another and pass environment variables that persist?
Setting Windows PowerShell environment variables - Stack Overflow
env and environment variables in Powershell
How can I constantly source a file with environment variables?
So if you've seen my username more than once on this platform you'll undoubtedly be familiar with my ecosystem of BATs, which I use to automate various functions on my home network, and which greatly speed the creative work that I do.
Many have rightly pointed out that PS1s would be a far better choice, and while I've agreed wholeheartedly, the simple fact is I haven't had the time to invest (yet) in converting to PS1s. The good news here is that 99% of these BATs are already formatted to use Powershell syntax, so the actual operations being performed I don't think will change very much.
But now comes the key piece that's been holding me back: a lot of the operations I use involve being able to set local and environmental variables like so:
@echo off & setlocal set "sourceDir=E:\SomeDir"
And reading them like so:
$sourceDir=$Env:sourceDir
And I'm a little unclear on how to make this work between PS1s. Here's what a typical operation looks like, with what I call a "super BAT" that backs up and cleans content from my main media drive:
-
Double click the starter BAT
-
The starter BAT file begins with defining three variables, before calling a series of other BATs, which we'll call "parent BATs" because they contain lists of "child BATs" that actually run operations
-
The parent BATs don't have variable definitions of their own, they just pass along the values they got from the starter BAT (with one exception--at one point the source and destination directories get swapped)
-
The child BATs inherit the values from the starter BAT, and run a bunch of operations in sequence, deleting temporary and junk files and copying files and folders all over the place
With this setup, I only have to define the first variables, and the BATs do the rest. The idea is to have a common set of commands that I can run, so I'm not re-creating the wheel every time I need to do something. All instances where I need to copy files using ROBOCOPY are defined in one BAT; all instances when I need to use MOVE-ITEM for the same task (because it can rename and ROBOCOPY can't as far as I know) are defined in another, but they're all set up to accept environmental variables supplied by the BATs I actually interact with.
/And MOVE-ITEM is just one...I have 46 child BATs in all to do various things, that I invoke in varying combinations to do specific things. I have commands that clear folders of everything but the newest file, to clear folders of ALL files, several variants that create new folders for different things, commands that identify and delete the largest or oldest or smallest files in a group...I could keep going, but you get the idea.
Combined, they help me keep my system clean and well-organized.
So for instance:
If the operation I want to launch is to do a full cleanup and backup of my main drive, this is what the process actually looks like:
-
___SUPERBAT__Full Operation_E-WSV.BAT
-
defines three variables
-
calls "Subroutines\___SUPERBAT Operation_001_Clear Debris.bat"
-
"___SUPERBAT Operation_001_Clear Debris.bat" defines search terms, then calls "call "%~dp0\Find and Delete Folders.bat"
-
"Find and Delete Folders.bat" inherits the values that came from the "full operation" BAT at the beginning plus the search terms from "001_Clear Debris.bat," and does its thing
-
"___SUPERBAT Operation_001_Clear Debris.bat" defines search terms, then calls "call "%~dp0\Find and Delete Files.bat"
-
"Find and Delete Files.bat" inherits the values that came from the "full operation" BAT at the beginning plus the search terms from "001_Clear Debris.bat," and does its thing
-
-
once all operations from SUPERBAT 001 are done, "___SUPERBAT__Full Operation_E-WSV.BAT" calls "Subroutines\___SUPERBAT Operation_002_Copy to H.bat", which invokes a different set of child BATs, which work off the same variables as SUPERBAT 001...
-
...and so on and so forth. For this one operation, there are a total of six numbered SUPERBAT files, and between them they invoke almost two dozen BATs...but because of how I've got it all set up, everything runs off just those three variables at the start.
So now my question is, how do I re-create this with Powershell scripts? I can convert all my child BATs to PS1s with no problem, but I need to key them to accept environmental variables like I did the BATs, and I need said variables to persist throughout the entire series of operations launched from the "Full Operation" file at the very beginning.
Does this make sense?
If sometime during a PowerShell session you need to see or to temporarily modify the PATH environment variable, you can type one of these commands:
Copy$env:Path # shows the actual content
$env:Path = 'C:\foo;' + $env:Path # attach to the beginning
$env:Path += ';C:\foo' # attach to the end
Changing the actual environment variables can be done by
using the env: namespace / drive information. For example, this
code will update the path environment variable:
Copy$env:PATH = "SomeRandomPath"; (replaces existing path)
$env:PATH += ";SomeRandomPath" (appends to existing path)
Making change permanent
There are ways to make environment settings permanent, but if you are only using them from PowerShell, it's probably a lot better to use Powershell profiles script.
Everytime a new instance of Powershell starts, it look for specific script files (named profile files) and execute them if they do exist. You can edit one of these profile to customize your enviroment.
To know where those profile scripts are located in your computer type:
Copy$profile
$profile.AllUsersAllHosts
$profile.AllUsersCurrentHost
$profile.CurrentUserAllHosts
$profile.CurrentUserCurrentHost
You can edit one of them, for example, by typing:
Copynotepad $profile