Created
September 26, 2015 03:40
-
-
Save sayedihashimi/8713078e789dd2274ab0 to your computer and use it in GitHub Desktop.
PowerShell Add-Path function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Based on: | |
| # http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/23/use-powershell-to-modify-your-environmental-path.aspx | |
| # Based on: | |
| # http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/23/use-powershell-to-modify-your-environmental-path.aspx | |
| function Add-Path{ | |
| [cmdletbinding()] | |
| param( | |
| [Parameter(Mandatory=$True,ValueFromPipeline=$True,Position=0)] | |
| [string[]]$pathToAdd | |
| ) | |
| process{ | |
| # Get the current search path from the environment keys in the registry. | |
| $oldpath=$env:path | |
| if (!$pathToAdd){ | |
| ‘No Folder Supplied. $env:path Unchanged’ | Write-Verbose | |
| } | |
| elseif (!(test-path $pathToAdd)){ | |
| ‘Folder [{0}] does not exist, Cannot be added to $env:path’ -f $pathToAdd | Write-Verbose | |
| } | |
| elseif ($env:path | Select-String -SimpleMatch $pathToAdd){ | |
| 'Folder already within $env:path' | Write-Verbose | |
| } | |
| else{ | |
| 'Adding [{0}] to the path' -f $pathToAdd | Write-Verbose | |
| $newpath = $oldpath | |
| # set the new path | |
| foreach($path in $pathToAdd){ | |
| $newPath += ";$path" | |
| } | |
| $env:path = $newPath | |
| [Environment]::SetEnvironmentVariable('path',$newPath,[EnvironmentVariableTarget]::Process) | Out-Null | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment