I am facing a problem in my login script written in powershell. I want to setup environemtn path vairable in User space. I first declare a new variable and then add it to the path variable at the end. the new variable doesn’t expand to its value in the path. following is the code
Idea is to update gat_path everytime i login, but update path only the first time. to append GAT_PATH only once to the path variable. After this whenever i update GAT_PATH, path should automatically have the new value.
$gat_path="S:\common\tools\sqlite;"
[environment]::SetEnvironmentVariable("GAT_PATH",$gat_path,[system.environmentvariabletarget]::User)
$res=$env:path.ToLower().Contains("s:\common") # if gat_path already added, then dont add again.
IF($res -eq "True")
{
echo "GAT Path is already set."
}
ELSE
{
[environment]::SetEnvironmentVariable("PATH",$env:path+";%GAT_PATH%",[system.environmentvariabletarget]::User)
}
In above code, GAT_PATH variable is created in the environemnt variables but the value of path is c:\python26\;%GAT_PATH%. GAT_PATH doesn’t expand to its actual value.
The reason i am using a variable to set path is because this script is run everytime i login. I dont want to add sqlite path at the end of path because it will keep adding sqlite at the end everytime i login. I want to be able to update GAT_PATH anytime in the script and, path should have the new value of GAT_PATH automatically.
Please note I cannot do the following as it creates the same problem as setting path directly without using a variable. Everytime i login, it will keep concatinating the gat_path to the path variable.
$gat_path="S:\common\tools\sqlite;"
[environment]::SetEnvironmentVariable("PATH",$env:path+";"+$gat_path,[system.environmentvariabletarget]::User)
What is the right way to use a variable in defining a path variable programatically using powershell?
Regards,
Alok
Instead of using
%GAT_PATH%you should use$env:gat_path