What's wrong on changing environment variables using
Environment provider like
$env:variableName = <variableValue> ?
(see
about_Environment_Variables - PowerShell | Microsoft Docs[
^]:
When you change environment variables in PowerShell, the change affects only the current session. This behavior resembles the behavior of the Set command in Windows-based environments and the Setenv command in UNIX-based environments.)
If a variable name is stored in a Powershell variable, you can utilize methods of the System.Environment class (see
Environment Class (System) | Microsoft Docs[
^]) to do something like
$Variable_Name = variableName;
[System.Environment]::SetEnvironmentVariable( $Variable_Name, <variableValue>);
In both occurrences, the
<variablevalue> could be a Powershell literal string e.g.
"some_path", or a string variable, e.g.
$FileContents.