Last active
September 20, 2015 19:33
-
-
Save tobiashochguertel/b4f4ed83055c5c1749d2 to your computer and use it in GitHub Desktop.
Add-Path Cmdlet
This file contains 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
function Add-Path { | |
<# | |
.SYNOPSIS | |
Adds a Directory to the Current Path | |
.DESCRIPTION | |
Add a directory to the current path. This is useful for temporary | |
changes to the path or, when run from your profile, for adjusting | |
the path within your powershell prompt. | |
.EXAMPLE | |
Add-Path -Directory "C:\Program Files\Notepad++" | |
.PARAMETER Directory | |
The name of the directory to add to the current path. | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter( | |
Mandatory=$True, | |
ValueFromPipeline=$True, | |
ValueFromPipelineByPropertyName=$True, | |
HelpMessage='What directory would you like to add?')] | |
[Alias('dir')] | |
[string[]]$Directory | |
) | |
PROCESS { | |
$Path = $env:PATH.Split(';') | |
foreach ($dir in $Directory) { | |
if ($Path -contains $dir) { | |
Write-Verbose "$dir is already present in PATH" | |
} else { | |
if (-not (Test-Path $dir)) { | |
Write-Verbose "$dir does not exist in the filesystem" | |
} else { | |
$Path += $dir | |
} | |
} | |
} | |
$env:PATH = [String]::Join(';', $Path) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment