-
-
Save jaw/4d1d858b87a5c208fbe42fd4d4aa97a4 to your computer and use it in GitHub Desktop.
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
# How to use | |
# | |
# First, you need to import the script: | |
# > Import-Module -Name "C:\Your\Path\to\EnvPaths.psm1" | |
# | |
# Add C:\Foo as the first path in the current session (goes away when you log out / close the window): | |
# > Add-EnvPathFirst -Path "C:\Foo" | |
# | |
# Add C:\Foo as the first path in the machine path (all users): | |
# > Add-EnvPathFirst -Path "C:\Foo" -Container Machine | |
# | |
# Remove any path matching *Foo* from the system paths for all users: | |
# > Remove-EnvPath -Path *Foo* -Container Machine | |
# | |
# Look for the existance of a wildcard path: | |
# > $present = Find-EnvPath -Path "*CMake\bin*" -Container Machine | |
# > if ([bool]$present) { Write-Host "found cmake binary path" } | |
function Add-EnvPathLast { | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] $Path, | |
[ValidateSet('Machine', 'User', 'Session')] | |
[string] $Container = 'Session' | |
) | |
if ($Container -ne 'Session') { | |
$containerMapping = @{ | |
Machine = [EnvironmentVariableTarget]::Machine | |
User = [EnvironmentVariableTarget]::User | |
} | |
$containerType = $containerMapping[$Container] | |
$persistedPaths = [Environment]::GetEnvironmentVariable('Path', $containerType) -split ';' | |
if ($persistedPaths -notcontains $Path) { | |
$persistedPaths = $persistedPaths + $Path | where { $_ } | |
[Environment]::SetEnvironmentVariable('Path', $persistedPaths -join ';', $containerType) | |
} | |
} | |
$envPaths = $env:Path -split ';' | |
if ($envPaths -notcontains $Path) { | |
$envPaths = $envPaths + $Path | where { $_ } | |
$env:Path = $envPaths -join ';' | |
} | |
} | |
function Add-EnvPathFirst { | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] $Path, | |
[ValidateSet('Machine', 'User', 'Session')] | |
[string] $Container = 'Session' | |
) | |
if ($Container -ne 'Session') { | |
$containerMapping = @{ | |
Machine = [EnvironmentVariableTarget]::Machine | |
User = [EnvironmentVariableTarget]::User | |
} | |
$containerType = $containerMapping[$Container] | |
$persistedPaths = [Environment]::GetEnvironmentVariable('Path', $containerType) -split ';' | |
if ($persistedPaths -notcontains $Path) { | |
$persistedPaths = ,$Path + $persistedPaths | where { $_ } | |
[Environment]::SetEnvironmentVariable('Path', $persistedPaths -join ';', $containerType) | |
} | |
} | |
$envPaths = $env:Path -split ';' | |
if ($envPaths -notcontains $Path) { | |
$envPaths = ,$Path + $envPaths | where { $_ } | |
$env:Path = $envPaths -join ';' | |
} | |
} | |
function Remove-EnvPath { | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] $Path, | |
[ValidateSet('Machine', 'User', 'Session')] | |
[string] $Container = 'Session' | |
) | |
if ($Container -ne 'Session') { | |
$containerMapping = @{ | |
Machine = [EnvironmentVariableTarget]::Machine | |
User = [EnvironmentVariableTarget]::User | |
} | |
$containerType = $containerMapping[$Container] | |
$persistedPaths = [Environment]::GetEnvironmentVariable('Path', $containerType) -split ';' | |
$persistedPaths = $persistedPaths | where { $_ -and $_ -notlike $Path } | |
[Environment]::SetEnvironmentVariable('Path', $persistedPaths -join ';', $containerType) | |
} | |
$envPaths = $env:Path -split ';' | |
# filter out the possible wildcard path | |
$envPaths = $envPaths | where { $_ -and $_ -notlike $Path } | |
$env:Path = $envPaths -join ';' | |
} | |
function Get-EnvPath { | |
param( | |
[Parameter(Mandatory=$true)] | |
[ValidateSet('Machine', 'User')] | |
[string] $Container | |
) | |
$containerMapping = @{ | |
Machine = [EnvironmentVariableTarget]::Machine | |
User = [EnvironmentVariableTarget]::User | |
} | |
$containerType = $containerMapping[$Container] | |
[Environment]::GetEnvironmentVariable('Path', $containerType) -split ';' | | |
where { $_ } | |
} | |
# returns True when the path is defined in the set of paths | |
function Find-EnvPath { | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] $Path, | |
[ValidateSet('Machine', 'User', 'Session')] | |
[string] $Container = 'Session' | |
) | |
if ($Container -ne 'Session') { | |
$containerMapping = @{ | |
Machine = [EnvironmentVariableTarget]::Machine | |
User = [EnvironmentVariableTarget]::User | |
} | |
$containerType = $containerMapping[$Container] | |
$persistedPaths = [Environment]::GetEnvironmentVariable('Path', $containerType) -split ';' | |
$persistedPaths = $persistedPaths | where { $_ -and $_ -like $Path } | |
return $persistedPaths -ne $null | |
} | |
$envPaths = $env:Path -split ';' | |
# filter out the possible wildcard path | |
$envPaths = $envPaths | where { $_ -and $_ -like $Path } | |
return $envPaths -ne $null | |
} | |
Export-ModuleMember -Function * |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment