This topic contains 5 replies, has 3 voices, and was last updated by
July 25, 2017 at 3:03 pm #75806
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 #75811ParticipantPoints: 154Rank: Participant
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 #75814
That's my exact question. How do I access the variables I set in powershell in batch?
July 25, 2017 at 3:36 pm #75821ParticipantPoints: 154Rank: Participant
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 #75829
Never mind. I got it. Created an environment variable via .NET
July 26, 2017 at 9:24 pm #75964ParticipantPoints: 0Rank: Member
I recently accomplished this. Your batch script needs to be able to accept parameters; take a look here.
set var=%1 echo %1
$var = value Invoke-Expression "& `"\path\to\batch`" $var"
I'm sure there are other ways. This is what I found and got to work.
The topic ‘Passing a variable from PowerShell to Batch?’ is closed to new replies.