I have one big batch script which sets bunch of environment variables. I want to call that batch script from powershell, that way I can get benefits of both i.e. enviorment variable set by my script and powershell.
The idea comes from this blog post: Nothing solves everything – PowerShell and other technologies Here is my version of this script. It calls a batch file (or any native command) and propagates its environment: UPDATE: Improved and better tested version of this script is here: Invoke-Environment.ps1
P.S. Here is the proposal to add similar capability to PowerShell: Extend dot sourcing concept to cmd files |
|||||
|
If you grab the PowerShell Community Extensions, there is an Invoke-BatchFile command in it that runs the batch file but more importantly, it retains any environment variable modifications made by the batch file e.g.:
|
|||||||||
|
Is it possible to convert your batch script to PowerShell script? If you run the bat file, its is executed in separate session that doesn't modify PowerShell's env variables. You can work with env variables very smoothly:
Or even (much shorter, returns only string):
You can change the environment path like this:
And you can even set environment variables at machine level like this:
|
|||
|
You can run a batch script from Powershell just by putting its name, but that won't help you. Environment variables set in the batch script will only be visible from that batch and anything that batch runs. Once the control returns back to Powershell the environment variables are gone. You could have the batch script run |
|||
|