Skip to content

Instantly share code, notes, and snippets.

@Hrxn
Last active January 19, 2024 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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')]
[System.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()]
[System.String] $Path,
[ValidateSet('Machine', 'User', 'Session')]
[System.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()]
[System.String] $Path,
[ValidateSet('Machine', 'User', 'Session')]
[System.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
}
}
# ---- 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