Skip to content

Powershell module to set or unset environment variables in the context of a single command, Unix style.

License

Notifications You must be signed in to change notification settings

nickcox/set-env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is it?

From an idea I came across on Twitter.

(1) A command, Set-Env, similar to the unix env command which sets or unsets environment variables, executes a given command, then restores them to their original state. For example, given the alias, Set-Alias env Set-Env:

> # Set-Alias env Set-Env
> env foo=bar { echo $env:foo }
bar

> echo $env:foo

> _

> env -u bat_theme -c { bat .\readme.md }
# unsets existing bat_theme variable
...

> env -i -c { bat .\readme.md }
# unsets all existing environment variables
...

(2) A CommandNotFoundAction, Register-AutoSetEnv, which enables a unix-like syntax for Set-Env:

> BAT_THEME="TwoDark" bat .\readme.md
...

> foo=bar baz=qux {node -e "['foo', 'baz'].forEach(x => console.log(process.env[x]))"}
bar
qux

Install

From the gallery

Install-Module set-env
Import-Module set-env

# add to profile. e.g:

Add-Content $PROFILE `n, 'Import-Module set-env'
Add-Content $PROFILE 'Register-AutoSetEnv'
Add-Content $PROFILE 'New-Alias env set-env'

Usage

Import the module and call Register-AutoSetEnv once if you want to use the VAR=something command syntax.

Note that if your command contains Powershell expressions like quotes or variables, you'll need to wrap it in a scriptblock (curly braces) to prevent Powershell from evaluating it too early.

# do this
tmp=xxx { echo $env:tmp }

# not this
tmp=xxx echo $env:tmp

About

Powershell module to set or unset environment variables in the context of a single command, Unix style.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published