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 *
@mkropat
Copy link
Author

mkropat commented Nov 5, 2019

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

@Hrxn
Copy link

Hrxn commented Nov 5, 2019

@mkropat Thank you, I appreciate it.

@simohakki
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
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"

@mrlee14
Copy link

mrlee14 commented Dec 5, 2021

When adding path to machine I get error: "Requested registry access is
not allowed"... is there some workaround?

@JohnyWS
Copy link

JohnyWS commented Jan 11, 2022

@mrlee14 it always requires admin access. This script can't change that.

@brucmao
Copy link

brucmao commented May 24, 2022

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

There was an error importing the module

Import-Module: C:\Users\brucm\Documents\PowerShell\Microsoft.PowerShell_profile.ps1:2
Line |
2 | Import-Module -Name "$HOME.config\powershell\EnvPaths.psm1"

 |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | File C:\Users\brucm\.config\powershell\EnvPaths.psm1 cannot be loaded. The file
 | C:\Users\brucm\.config\powershell\EnvPaths.psm1 is not digitally signed. You cannot run this script on
 | the current system. For more information about running scripts and setting execution policy, see
 | about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.

this only can work after I execute this command with Administrator
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser

@Hrxn
Copy link

Hrxn commented May 24, 2022

@brucmao yes, this is normal. On a fresh install, ExecutionPolicy is not set, which means that PowerShell is not allowed to run any scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment