Passing a variable from PowerShell to Batch?

Welcome Forums General PowerShell Q&A Passing a variable from PowerShell to Batch?

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
1 year, 3 months ago.

  • Author
    Posts
  • #75806

    Participant
    Points: 0
    Rank: Member

    Hello,

    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 ?

    Mike

  • #75811

    Participant
    Points: 154
    Helping Hand
    Rank: 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.

  • #75814

    Participant
    Points: 0
    Rank: Member

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

  • #75821

    Participant
    Points: 154
    Helping Hand
    Rank: 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.

  • #75829

    Participant
    Points: 0
    Rank: Member

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

  • #75964

    Participant
    Points: 0
    Rank: Member

    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.

The topic ‘Passing a variable from PowerShell to Batch?’ is closed to new replies.