Passing a variable from PowerShell to Batch?

This topic contains 5 replies, has 3 voices, and was last updated by  Craig 10 months ago.

  • Author
  • #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 ?


  • #75811

    Olaf Soyk

    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.

  • #75814


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

  • #75821

    Olaf Soyk

    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.

  • #75829


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

  • #75964


    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.

You must be logged in to reply to this topic.