Calling powershellscript from batch file

winprofessional 41 Reputation points
2020-04-01T16:02:48.29+00:00

I am trying to call a powershell script that contains a function that returns boolean, from a windows batch file. But I am not able to get the return value from the power shell script in the batch file. I found a couple of posts but couldn't get an answer . Below is the outline of code.

Below is the Powershell script that has a function only(Script1.ps1)

Function Get-Value
{

 $isOK = $false
 ...
  return $isOK
}

Below is the batch file that calls the above function

@echo off
set val=powershell -ExecutionPolicy ByPass -File .\Script1.ps1
@echo %val%
pause

How can I receive the Boolean value in the batch file and use. it. I am not mush proficient in batch file/ps scripting

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,517 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Dave Patrick 425.7K Reputation points MVP
    2020-04-01T16:30:14.217+00:00

    QnA currently supports the products listed in right-hand pane (more to be added) Better to reach out to subject matter experts in dedicated forums over here.

    https://social.technet.microsoft.com/Forums/windows/en-US/home?forum=winserverpowershell

    https://social.technet.microsoft.com/Forums/scriptcenter/en-us/home?forum=ITCG

    (please don't forget to mark helpful replies as answer)

    0 comments No comments

  2. Richard Mueller 366 Reputation points
    2020-04-01T16:38:52.807+00:00

    For one thing, you never invoke the function in your PowerShell script. Add the statement "Get-Value" at the end.
    I use code similar to below in a bat file to run a PowerShell script:

    @Echo off
    PowerShell -Command "& {.\Script1.ps1}"

    Finally, you should ask your question in the PowerShell forum.

    https://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell