How to call an executable with parameters from powershell script

powershell, powershell-2.0

Solution

Don't make a command string. Simply use the call operator (`&`):

Write-Host 'Get data from service'

$path = 'D:\DataService'

Push-Location $path

$Date     = Get-Date
$DateFrom = $Date.ToString('yyyy-MM-dd HH:mm:ss')
$DateTo   = $Date.AddDays(-1).ToString('yyyy-MM-dd')

& ReportGen.exe -ReportType Data -DateFrom $DateFrom $DateTo

Problem

I am seeking help on how to call cmd with specific parameters from a powershell script. So far what I have written is below, but it gives me an error message saying $_cmd is not recognized. I am trying to pass the from date and to date to a an exe... The from date as you can see needs to be today - 1 and the to date should be now. The path of the executable is D:\DataService and that's why I am setting the path early in the script. ``` Write-Host "Get data from service" $path ="D:\DataService" Push-Location $path $Date = Get-Date $DateFrom = $Date.ToString("yyyy-MM-dd HH:mm:ss") $DateTo = $Date.AddDays(-1).ToString("yyyy-MM-dd") $_cmd = "ReportGen.exe -ReportType Data -DateFrom $DateFrom $DateTo" %$_cmd% ``` Any suggestions please?

Original source