Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the return value of an application in PowerShell?

Tags:

powershell

I am passing an argument to my test application via powershell. I wish to get the return value of the application once it finishes.

How can I get the return value of the application instead of the console output.

I'm running the application by running

Test.ps1 2

Test.ps1

param ([string]$param1)
    
$path = "C:\Workspaces\myapplication\"
$executable = "Test.exe"
    
$filepath  = "$($path)$($executable) $($param1)"
    
Try 
{
    $Result = iex $filepath
        
    #this writes out the console output of Test.exe instead of the return value.
    Write-Host $Result  
    Write-Host $LASTEXITCODE  
}
Catch
{
    Write-Host "Exit Code"
    Write-Host $LASTEXITCODE    
}
like image 928
agent_bean Avatar asked Sep 01 '25 03:09

agent_bean


1 Answers

you should use Start-Process:

$p = Start-Process $($path)$($executable) -ArgumentList $input
$p.HasExited
$p.ExitCode
like image 187
Stefan Avatar answered Sep 02 '25 21:09

Stefan