Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 *
@crupest

This comment has been minimized.

Copy link

crupest commented Jul 28, 2018

I really like this. But I have a question about the script. Can you tell me what | where { $_ } means?

@iuooip

This comment has been minimized.

@Hrxn

This comment has been minimized.

Copy link

Hrxn commented Nov 2, 2019

Really great, extremely useful. Kudos.

But @mkropat, how about adding Export-ModuleMember statements at the end of the file?

No functional change, I'm aware. But it's, well, recommended. Quoting from the docs:

An Export-ModuleMember command is optional, but it is a best practice. Even if the command confirms the default values, it demonstrates the intention of the module author.

@mkropat

This comment has been minimized.

Copy link
Owner Author

mkropat commented Nov 5, 2019

@Hrxn sure, that's an easy change. It's done.

@Hrxn

This comment has been minimized.

Copy link

Hrxn commented Nov 5, 2019

@mkropat Thank you, I appreciate it.

@simohakki

This comment has been minimized.

Copy link

simohakki commented Jan 21, 2020

hi, i want to call Add-EnvPath function from a powershell to add 'c:/foo', can you tell me how can i do that please ?

@Hrxn

This comment has been minimized.

Copy link

Hrxn commented Jan 21, 2020

@simohakki

If you use Add-EnvPath like this:
Add-EnvPath "C:\Foo" it will simply add that path to the current session, and that will work (for the current session).
Note that Add-EnvPath can take two parameters, [string] $Path and [string] $Container. If you don't set the second parameter ($Container) it will always default to Session.
If you want to add C:\Foo to either the User or System Environment var, do this:
Add-EnvPath "C:\Foo" "User"
or, alternative syntax:
Add-EnvPath -Path "C:\Foo" -Container "Machine"

Add-EnvPath (and the other functions) only work if you import them first, of course, so do this before (or in your profile, for example):
Import-Module -Name "C:\Your\Path\to\EnvPaths.psm1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.