Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell add to path, temporarily

Tags:

powershell

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!

like image 625
Simon Ordo Avatar asked Jun 06 '16 17:06

Simon Ordo


People also ask

How do I add to a path in PowerShell?

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.

What does $ENV mean in PowerShell?

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.

How do I add something to my path?

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.

How do I set an environment variable in PowerShell?

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.


2 Answers

Option 1: Modify the $env:Path Variable

  1. Append to the Path variable in the current window:

    $env:Path += ";C:\New directory 1;C:\New directory 2" 
  2. Prefix the Path variable in the current window:

    $env:Path = "C:\New directory 1;C:\New directory 2;" + $env:Path 
  3. Replace the Path variable in the current window (use with caution!):

    $env:Path = "C:\New directory 1;C:\New directory 2" 

Option 2: Use the editenv Utility

I 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.

like image 56
Bill_Stewart Avatar answered Sep 19 '22 12:09

Bill_Stewart


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); 
like image 37
Bacon Bits Avatar answered Sep 20 '22 12:09

Bacon Bits