Skip to content

Instantly share code, notes, and snippets.

@Hrxn
Last active August 2, 2024 02:29
Show Gist options
  • Save Hrxn/57c83ab93d642bdd868f97355f3aab45 to your computer and use it in GitHub Desktop.
Save Hrxn/57c83ab93d642bdd868f97355f3aab45 to your computer and use it in GitHub Desktop.
PowerShell: module for the PATH system environment variable
# PowerShell Module File
# Name: EnvPaths.psm1
# Desc: Module for handling of the 'Path' system environment variable
# Source: https://gist.github.com/Hrxn/57c83ab93d642bdd868f97355f3aab45; Based on: https://gist.github.com/mkropat/c1226e0cc2ca941b23a9
function Get-EnvPath {
param(
[Parameter()]
[ValidateSet('Machine', 'User', 'Session')]
[string] $Container = 'Session'
)
$ContainerMapping = @{
Machine = [System.EnvironmentVariableTarget]::Machine
User = [System.EnvironmentVariableTarget]::User
}
$ContainerType = $ContainerMapping[$Container]
switch ($Container) {
'Session' {
$Env:Path -split [System.IO.Path]::PathSeparator | Where-Object { $_ }
}
default {
[System.Environment]::GetEnvironmentVariable('Path', $ContainerType) -split [System.IO.Path]::PathSeparator | Where-Object { $_ }
}
}
}
function Add-EnvPath {
param(
[Parameter(Mandatory)]
[ValidateNotNullOrWhiteSpace()]
[string] $Path,
[ValidateSet('Machine', 'User', 'Session')]
[string] $Container = 'Session'
)
if ($Container -ne 'Session') {
$ContainerMapping = @{
Machine = [System.EnvironmentVariableTarget]::Machine
User = [System.EnvironmentVariableTarget]::User
}
$ContainerType = $ContainerMapping[$Container]
$PersistedPaths = [System.Environment]::GetEnvironmentVariable('Path', $ContainerType) -split [System.IO.Path]::PathSeparator
if ($PersistedPaths -notcontains $Path) {
$PersistedPaths = $PersistedPaths + $Path | Where-Object { $_ }
[System.Environment]::SetEnvironmentVariable('Path', $PersistedPaths -join [System.IO.Path]::PathSeparator, $ContainerType)
}
}
$EnvPaths = $Env:Path -split [System.IO.Path]::PathSeparator
if ($EnvPaths -notcontains $Path) {
$EnvPaths = $EnvPaths + $Path | Where-Object { $_ }
$Env:Path = $EnvPaths -join [System.IO.Path]::PathSeparator
}
}
function Remove-EnvPath {
param(
[Parameter(Mandatory)]
[ValidateNotNullOrWhiteSpace()]
[string] $Path,
[ValidateSet('Machine', 'User', 'Session')]
[string] $Container = 'Session'
)
if ($Container -ne 'Session') {
$ContainerMapping = @{
Machine = [System.EnvironmentVariableTarget]::Machine
User = [System.EnvironmentVariableTarget]::User
}
$ContainerType = $ContainerMapping[$Container]
$PersistedPaths = [System.Environment]::GetEnvironmentVariable('Path', $ContainerType) -split [System.IO.Path]::PathSeparator
if ($PersistedPaths -contains $Path) {
$PersistedPaths = $PersistedPaths | Where-Object { $_ -and $_ -ne $Path }
[System.Environment]::SetEnvironmentVariable('Path', $PersistedPaths -join [System.IO.Path]::PathSeparator, $ContainerType)
}
}
$EnvPaths = $Env:Path -split [System.IO.Path]::PathSeparator
if ($EnvPaths -contains $Path) {
$EnvPaths = $EnvPaths | Where-Object { $_ -and $_ -ne $Path }
$Env:Path = $EnvPaths -join [System.IO.Path]::PathSeparator
}
}
# [Section] Exporting Module Content
Export-ModuleMember -Function Get-EnvPath, Add-EnvPath, Remove-EnvPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment