Pass variable from batch to powershell

13,559

Solution 1

If you have a file.ps1 that takes a parameter,

param($asset)
"The asset tag is $asset"

You can pass in the variable as an argument.

SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%file.ps1
SET /p asset=Enter the asset name

PowerShell -NoProfile -ExecutionPolicy Bypass -file "%PowerShellScriptPath%" "%asset%"

Solution 2

You can use $env:variable_name syntax to access curent cmd environment variables from powershell. To get hold of your variable you'd use $env:asset
To try, open cmd, do set "myvar=my value", start powershell, do $env:myvar (this will simply print it, but of course you can use it as any other ps variable)

Just as a sidenote, ps has good help system. If you do help env it will list two relevant topics which you can examine in turn to get detailed information.

Hope this helps

Share:
13,559
rangerr
Author by

rangerr

Updated on July 23, 2022

Comments

  • rangerr
    rangerr almost 2 years

    I have a batch file that ask the user for a variable line set /p asset=. Im calling my powershell script like this

    SET ThisScriptsDirectory=%~dp0

    SET PowerShellScriptPath=%ThisScriptsDirectory%file.ps

    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";

    Im wondering how i send powershell the variable 'asset' from my batch file.

    Here is my .bat file content

    @Echo off  
    cls
    Color E
    cls
    
    @echo Enter asset below
    set /p asset=
    
    @echo.
    @echo your asset is %asset%
    @echo.
    goto startusmt
    
    :startusmt
    @echo.
    @echo executing psexec ...
    @echo.
    
    SET ThisScriptsDirectory=%~dp0
    SET PowerShellScriptPath=%ThisScriptsDirectory%RemoteUSMT.ps1
    PowerShell -NoProfile -ExecutionPolicy Bypass -file %PowerShellScriptPath% %asset%
    psexec \\%asset% -u domain\username -p password cmd 
    
    goto EOF
    
    :EOF 
    PAUSE
    
  • Rynant
    Rynant about 10 years
    File runs the PowerShell script. You can use -file in place of calling the file from -command.
  • rangerr
    rangerr about 10 years
    PowerShell -NoProfile -ExecutionPolicy Bypass -file "%PowerShellScriptPath%" %asset% Like this?
  • rangerr
    rangerr about 10 years
    How do I reference that variable in powershell?
  • Rynant
    Rynant about 10 years
    In the example that I gave, the value of %asset% is passed to the $FileName variable. You can use whatever variable name you want; the point is that the arguments listed in the batch file get passed to the param variables in the .ps1 script. If your .ps1 took two parameters (e.g. param($one, $two); write-host "one: $one, two: $two") you could call it from a batch file with PowerShell -file %PowerShellScriptPath% %assetA% %assetB%
  • rangerr
    rangerr about 10 years
    This is what I have right now and the powershell line wont display my variable asset when I try to call it. bath file line is PowerShell -NoProfile -ExecutionPolicy Bypass -file %PowerShellScriptPath% %asset% Then in my .ps file I have param($asset) Get-Item $asset "The asset tag is $asset" . But the script never puts the asset I typed in the .bat file
  • Rynant
    Rynant about 10 years
    @rangerr You don't want Get-Item $asset in your PowerShell script. I only used it as an example in my script.
  • rangerr
    rangerr about 10 years
    even with the Get-Item $asset left out im still not able to have the $asset in powershell to to the %asset% I defined in the .bat file.
  • Rynant
    Rynant about 10 years
    Yes this is an option, but generally it's better to have arguments that you pass in to the PS script so that you can tell by the function signature what the script depends on for input. It also makes the PowerShell script more usable if calling directly from PowerShell.
  • Rynant
    Rynant about 10 years
    Have you tried the example exactly as it is in my answer? It works for me. Maybe you are doing something differently in your bat file.
  • rangerr
    rangerr about 10 years
    my .ps file only has your 2 lines plus "The asset tag is $asset. my .bat file looks like my first post but with your edit to the powershell line
  • Rynant
    Rynant about 10 years
    The PowerShell script should be .ps1 not .ps
  • rangerr
    rangerr about 10 years
    Sorry yes its called "RemoteUSMT.ps1" and in side its param($asset) and "The asset tag is $asset
  • wmz
    wmz about 10 years
    @Rynant Generally yes, but sometime somewhere you may find you have control over the script but not it's invocation/environment and then it comes in handy :-)
  • Rynant
    Rynant about 10 years
    Hmm, that batch file works for me; I see 'the asset tag is <text I entered>'. Are you seeing PowerShell only outputting: "the asset tag is "?
  • rangerr
    rangerr about 10 years
    Yes its only saying "the asset tag is " and not inputting the $asset variable
  • rangerr
    rangerr about 10 years
    I'll post an answer below with my batch file content if that helps
  • Rynant
    Rynant about 10 years
    I tried the .bat text from your updated post and it worked for me, so I'm stumped as to why it's not working. You may want to put quotes around both variables on the powershell call in the batch file in case there is a space in the path or the user's input, but I don't think that is the problem if powershell outputs something. Have you tried @wmzs answer? It would be interesting to hear if that works. Just make "The asset tag is " + $env:asset` the only line in your .ps1 file.
  • rangerr
    rangerr about 10 years
    That work. Im not sure why that way works and the other way doesnt but at least it works now. Thank you for helping me figure this out!
  • Kenny83
    Kenny83 over 4 years
    @Rynant What if I'm calling powershell like this: powershell -Command "Remove-Item %filePath%\* -Recurse -Exclude '*.avi','*.mkv','*.mp4'"? I.e. not using a script, but simply invoking a single command? This doesn't seem to work for me :-/ I've also tried wrapping the command in braces like so &{command}, separating the command into its own batch variable, using setlocal and all sorts of weird hacks, but no dice. This seems to be such a simple thing to do but really isn't for a batch and powershell n00b like me!