Last active
January 19, 2024 10:12
-
-
Save mkropat/c1226e0cc2ca941b23a9 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
function Add-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 ';' | |
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 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 ';' | |
if ($persistedPaths -contains $Path) { | |
$persistedPaths = $persistedPaths | where { $_ -and $_ -ne $Path } | |
[Environment]::SetEnvironmentVariable('Path', $persistedPaths -join ';', $containerType) | |
} | |
} | |
$envPaths = $env:Path -split ';' | |
if ($envPaths -contains $Path) { | |
$envPaths = $envPaths | where { $_ -and $_ -ne $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 { $_ } | |
} | |
Export-ModuleMember -Function * |
Recommended change to Get-EnvPath:
function Get-EnvPath {
param(
[Parameter(Mandatory=$true)]
[ValidateSet('Machine', 'User', 'Session')]
[string] $Container
)
$containerMapping = @{
Machine = [EnvironmentVariableTarget]::Machine
User = [EnvironmentVariableTarget]::User
}
$containerType = $containerMapping[$Container]
switch( $Container ){
"Session" {
$env:Path -split ";" |
where { $_ }
}
default {
[Environment]::GetEnvironmentVariable('Path', $containerType) -split ';' |
where { $_ }
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It filters out any records that is regarded as "falsy" values, including empty and null values.