Skip to content

Instantly share code, notes, and snippets.

@tobiashochguertel
Last active September 20, 2015 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiashochguertel/b4f4ed83055c5c1749d2 to your computer and use it in GitHub Desktop.
Save tobiashochguertel/b4f4ed83055c5c1749d2 to your computer and use it in GitHub Desktop.
Add-Path Cmdlet
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