Last active
August 2, 2024 00:35
-
-
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 * |
@mkropat Can you please tell me what is the benefit of this filtering
| where { $_ }
?
Wouldn't the result be exactly the same without this?
It filters out any records that is regarded as "falsy" values, including empty and null values.
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 { $_ }
}
}
}
instead of hardcode ";"
Would you mind replacing it with [IO.Path]::PathSeparator
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mkropat Can you please tell me what is the benefit of this filtering
| where { $_ }
?Wouldn't the result be exactly the same without this?