Author Posts

July 25, 2017 at 3:03 pm


I'm calling a Batch script from PowerShell with Start-Process, however before Start-Process, I need to match up the variables in both PowerShell and Batch so once I change the path to the file once in PowerShell, it changes in both places (PowerShell and batch at the same time). Let me give an example;

Let's call the path 'C:\Documents\Library', and that path will be fed into a variable. $PathToFile = 'C:\Documents\Library'

The above path is being called in both places. PowerShell and Batch. Now let's say that path changes from 'C:\Documents\Library' to 'C:\Library\MyFiles'

Can I change that path in PowerShell, and it also change in batch automatically via the variable $PathToFile ?


July 25, 2017 at 3:22 pm

I'm not completely sure if I got what you are talking about. But let me ask something to make it more clear. Why do you use batch from Powershell? That should not be necessary at all. Why don't you continue with Powershell? How do you access variables you set in Powershell in a batch script? It might be helpful to see your actual code.

July 25, 2017 at 3:26 pm

That's my exact question. How do I access the variables I set in powershell in batch?

July 25, 2017 at 3:36 pm

OK. I got this now. 😉 But why do you use a batch script at all? Powershell is much more powerfull (that's why they choose the name I guess) and IMHO much easier.

July 25, 2017 at 3:53 pm

Never mind. I got it. Created an environment variable via .NET

July 26, 2017 at 9:24 pm

I recently accomplished this. Your batch script needs to be able to accept parameters; take a look here.

Batch side:

set var=%1
echo %1

Powershell side:

$var = value
Invoke-Expression "& `"\path\to\batch`" $var"

I'm sure there are other ways. This is what I found and got to work.