PowerShell support #124

zimbatm opened this Issue May 24, 2014 · 12 comments


TODO in direnv 3

5 participants

zimbatm commented May 24, 2014

Alternatively to Cygwin, it should be possible to support the Microsoft PowerShell. The only unclear requirement that needs to be researched is if PowerShell would support evaluating code before each prompt.

  • Check how eval works and if there is a hook for executing direnv before each prompt
  • Install bash trough http://mingw.org/wiki/msys
  • Add a new shell_powershell.go that supports generating set instructions from the env diff.
@zimbatm zimbatm added this to the Cygwin support milestone May 24, 2014
@zimbatm zimbatm added the Feature label May 24, 2014

To support window's terminals, 3 types of scripts need to be executed:

  1. script.ps1
  2. script.bat
  3. script.cmd

Powershell runs .ps scripts, CMD should be used to run bat or cmd scripts. There are some minor differences between .bat and .cmd: http://stackoverflow.com/questions/148968/windows-batch-files-bat-vs-cmd (cmd is recommended script format now)

In order to set environment variables in CMD you need to use SET:

SET blah=blah
echo %cmd%

In order set environment variables in powershell (it uses a different syntax) see:

When using Cygwin, any environment variables set in Powershell or CMD are transferred to Cygwin child shell or vice versa.

So I can do this in Cygwin:

export EDITOR="nano"
CMD /C "echo %EDITOR%"

Which echos out nano.


Which means you may need shell_powershell.go and shell_cmd.go. Most windows installations have powershell now, but older installations may only have CMD. I think XP and up?


The starting area for PowerShell scripts is here:


As explained here: http://technet.microsoft.com/en-au/library/ee692764.aspx

However in order to run ps1 scripts, one has to activate their execution policy:

Here's my profile powershell:

Set-Location $env:USERPROFILE

CMD does not automatically look for any kind of profile script to run. People will need to hook in a script manually upon starting CMD: http://blog.cachemiss.com/articles/My%20cmd.exe%20Profile.pod

zimbatm commented May 26, 2014

Interesting, thanks for all the material. CMD support is probably too legacy and require too much effort/hacks to work properly.

Apart from msys I also found an independent port of bash: http://win-bash.sourceforge.net/ Not sure what it's worth though.

Found this article on how to sign script: http://blogs.technet.com/b/heyscriptingguy/archive/2010/06/17/hey-scripting-guy-how-can-i-sign-windows-powershell-scripts-with-an-enterprise-windows-pki-part-2-of-2.aspx . According to http://technet.microsoft.com/en-us/magazine/2008.04.powershell.aspx?pr=blog I could also get a SSL cert for direnv.net and use it to sign the script.


For PowerShell, it looks like worst-case a custom Prompt function could look up the direnv stuff, and then repeat the default prompt behavior: https://technet.microsoft.com/en-us/library/hh847739.aspx

A quick search isn't revealing any other kind of hooking capabilities, but since the prompt is generated by running a function, that's a natural place to do it.

zimbatm commented Apr 20, 2016

Cool, thanks for looking that up. Are you planning on working on this further ? I would be keen on adding first-class windows support but don't have a development environment.


@zimbatm I'm not sure at present. For one thing, I do not know any Go. I have also been considering implementing DirEnv's core functionality directly in PowerShell.

I am very interested in having something like direnv in PowerShell, and am willing to do some work to get it. So far, have mostly just been trying to document what I know or learn while investigating whether it was already supported, to help whoever does eventually implement it.

zimbatm commented Apr 20, 2016

Alright that's cool too. The core logic is inside of cmd_export.go which is executed before every shell prompt. Basically it looks for the file and stores a diff inside of an environment variable. If you have a facility to keep a thread running inside of PowerShell you might not have to spawn a new process on each prompt and do that. Also bash is a requirement for direnv which might not fit very well with the windows environment.


If Bash is an internal requirement, it seems to me that it'd likely be better for direnv to focus on supporting Unix-like environments (Cygwin, MSYS, etc.) and for a separate project to enable similar functionality in native PowerShell.

zimbatm commented Apr 21, 2016

That's what's used to parse and execute the .envrc files. I wanted to keep the bash syntax and not re-invent a language. I bet it's possible to ship bash as a standalong executable alongside direnv in windows but yeah, something else might be preferable for PowerShell.


Hey All,

I'd be interested in this on windows as well... Not sure if it helps, but wrote a custom cd command in PowerShell which give it some fun features. Maybe this could be used as an example on how to hook into the directory changing...

Code here: https://github.com/staxmanade/DevMachineSetup/blob/master/GlobalScripts/Change-Directory.ps1
Some blog posts describing the features here, here, and here

Also, not sure if I have time to work on it as I spend more time in Mac than windows - but if I jump on windows and run into some time I'll see what can be done.

mcandre commented Jun 11, 2016


@zimbatm zimbatm added the Windows label Jul 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment