2

I am calling a powershell script from a batch file, both in different locations.

I would like to pass the folder location for the powershell script file as well as the parameter, a user entered string in the batch file, from that batch file.

powershell script:

$url = "https://www.ons.gov.uk/generator?format=csv&uri=/economy/inflationandpriceindices/timeseries/chaw/mm23"
$output="sample.csv"
$start_time = Get-Date
$arg1=$args[0]
Invoke-WebRequest -Uri $url -OutFile $arg1\$output
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

My Batch file :

powershell.exe -ExecutionPolicy Bypass -file C:\temp\download-rpi.ps1 "\\drives\savehere"
6
  • set /p Commented Jan 26, 2018 at 12:43
  • Why do you need a batch file at all? Just run your script from the PowerShell prompt. Commented Jan 26, 2018 at 14:34
  • @Bill_Stewart Because he can only call batch from the 3rd party software. Commented Jan 27, 2018 at 17:07
  • Perhaps, but that is not stated in the question. Commented Jan 27, 2018 at 18:14
  • @Bill_Stewart He did not, no, he only mentioned in comments below in answers. Commented Jan 29, 2018 at 12:43

2 Answers 2

3

You could have done all of this in a single language instead of using both Powershell and batch, but regardless, here is what you want

@echo off
if "%1"=="" (
    set /p "pspath=Enter the path to Powershell: "
    ) else set "pspath=%1"
if "%2"=="" (
    set /p "sharepath=Enter the share parameter: "
    ) else set "sharepath=%2"
    powershell.exe -ExecutionPolicy Bypass -file "%pspath% "%sharepath%"

How it works:

you can either double click the file which will then prompt for the powershell path and sharepath

OR

run from cmdline and enter the variables after the batch command, which will use %1 %2 to set the variables. Examples:

  1. Double Clicking batch:
Enter the path to Powershell: C:\Some Path\
Enter The share parameter: \\some\share

Result

powershell.exe -ExecutionPolicy Bypass -file "C:\Some Path\" "\\some\share"
  1. Running from cmd.exe prompt
C:\> BatchFileName.cmd "C:\Some Path\" "\\some\share"

Result

powershell.exe -ExecutionPolicy Bypass -file "C:\Some Path\" "\\some\share"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It helped me to fix my problem
@Khushi glad I could help.
1

In PowerShell, this is done using parameters:

param(
    [string]$Path
)

$url        = "https://www.ons.gov.uk/generator?format=csv&uri=/economy/inflationandpriceindices/timeseries/chaw/mm23"
$output     = "sample.csv"
$start_time = Get-Date

Invoke-WebRequest -Uri $url -OutFile $Path\$output

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

An alternative is to use the automatic variable $MYINVOCATION to get similar behaviour to an $args array but I would not recommend that as you have no way of knowing what unbound parameters will be provided.

$url        = "https://www.ons.gov.uk/generator?format=csv&uri=/economy/inflationandpriceindices/timeseries/chaw/mm23"
$output     = "sample.csv"
$start_time = Get-Date
$Path       = $MYINVOCATION.UnboundArguments

Invoke-WebRequest -Uri $url -OutFile $Path\$output

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

3 Comments

@gmsoylman : Thanks. now coming to batch file - instead of passing hardcoded path(as i passed in batch file for $path) how to pass them as parameters which can be provided at third party ? Also my batch file is in different folder so i would like to give the path of poweshell script as parameter
@Khushi Is there a reason you're using a batch file instead of pure PowerShell?
Yes. I am calling this batch file in a third part application which does not support calling powerscript file directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.