I'm trying to write a simple PowerShell script to deploy a Visual Studio ASPNET Core 1 project.
Currently, in a batch file i can say
Path=.\node_modules\.bin;%AppData%\npm;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External;%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External\git
That will modify the path variable for the duration of the session... for the life of me I can't figure out how to do this simple thing in powershell.
Can someone help me translate this into powershell?
TIA!
To add to the PATH, append a semicolon and a new path on the end of the long path string. We can use PowerShell to check whether the path we want to add is already in the existing path.
The $env:PATHEXT variable contains a list of file extensions that Windows considers to be executable files. When a script file with one of the listed extensions is executed from PowerShell, the script runs in the current console or terminal session.
To add a path to the PATH environment variableIn the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.
To set the environmental variable using PowerShell you need to use the assignment operator (=). If the variable already exists then you can use the += operator to append the value, otherwise, a new environment variable will be created.
$env:Path
VariableAppend to the Path
variable in the current window:
$env:Path += ";C:\New directory 1;C:\New directory 2"
Prefix the Path
variable in the current window:
$env:Path = "C:\New directory 1;C:\New directory 2;" + $env:Path
Replace the Path
variable in the current window (use with caution!):
$env:Path = "C:\New directory 1;C:\New directory 2"
editenv
UtilityI wrote a Windows command-line tool called editenv
that lets you interactively edit the content of an environment variable. It works from a Windows console (notably, it does not work from the PowerShell ISE):
editenv Path
This can be useful when you want to edit and rearrange the directories in your Path
in a more interactive fashion, and it affects only the current window.
You can just use $env:Path
, but if you're anxious that that isn't explicit enough, you can use System.Environment.SetEnvironmentVariable()
:
[System.Environment]::SetEnvironmentVariable('Path',$Value,[System.EnvironmentVariableTarget]::Process);
And GetEnvironmentVariable() can explicitly retrieve:
[System.Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::Process); [System.Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::Machine); [System.Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::User);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With