Skip to content

Instantly share code, notes, and snippets.

@mkropat
Last active August 2, 2024 00:35
Show Gist options
  • Save mkropat/c1226e0cc2ca941b23a9 to your computer and use it in GitHub Desktop.
Save mkropat/c1226e0cc2ca941b23a9 to your computer and use it in GitHub Desktop.
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 *
@CuriousTreeFrog
Copy link

@mkropat Can you please tell me what is the benefit of this filtering | where { $_ } ?
Wouldn't the result be exactly the same without this?

@JohnyWS
Copy link

JohnyWS commented Nov 5, 2022

@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.

@anonhostpi
Copy link

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 { $_ }
        }
    }
}

@davidkhala
Copy link

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