I have multiple folders containing different python projects. Each project has its own virtual environment that I activate before working on it. I activate the virtual environment by running in the project directory:


Is there a way to automatically run this command once I cd into a directory?

  • Just remove your cmd.exe references and ask for PowerShell only, then it's not a duplicate anymore and has a different answer. – Peter Hahndorf Aug 14 '16 at 22:40

Are you adverse to typing something other than "cd" in? If not, then you could make a PowerShell function that changes location and runs the batch file. Depending on what the batch file does, you may even be able to remove the batch file(s) and just code it into the function. Put the function into your PowerShell profile, and it'll be available every time you open a console.

Or, given that "cd" in PowerShell is just an alias for "Set-Location", you could change the alias to your function, but that'd mean that if you try to change directories in PowerShell for something other than your projects, it'd throw an error or do things that you may not want, depending on how you go about it.

Note that this would have no effect on the "cd" in Command Prompt; this would only change things within PowerShell.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.