Link to home
Start Free TrialLog in
Avatar of YZlat
YZlatFlag for United States of America

asked on

Pass a parameter to a powershell file from batch file

I need to run a Powershell ps1 file from batch file and pass it one paramter - file path: C:\Test\My Folder\test.ps1

What's the best way to do that?
Avatar of KenMcF
KenMcF
Flag of United States of America image

Can you post the powershell script?

you should  be able to run it from a batch file just like running from a powershell console. in the batch try somethign like this followed by any paramenters you need.

powershell.exe  C:\Test\My Folder\test.ps1



Avatar of YZlat

ASKER

do i need full path to powershell file in my batch?

and how do I pass a paramteter to powershell?  A parameter is a file path to another file C:\Temp\temp_file.bmp


ASKER CERTIFIED SOLUTION
Avatar of KenMcF
KenMcF
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of YZlat

ASKER

My script has no problem, I do not need to do anything to it, what I need is to call my script from a batch file and pass it a parameter
ok, then depending on what parameter needs past you can do what I posted before. It will all depend on how the script is written.
Avatar of YZlat

ASKER

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Test\My Folder\test.ps1 -file c:\temp\temp_file.bmp



didn't wotk, since it would capture the "-file" as a first argument.  

I am calling my batch file from another program and I send it a file path as a string, so my batch has the following code:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Test\My Folder\test.ps1 %1

and it works if the file passed to batch does not contain spaces, otherwise I need single quotes around it

for instance

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Test\My Folder\test.ps1 %1

only caputres part of the file: C:\Program

so I need to run

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Test\My Folder\test.ps1 'C:\program Files\Test\my_file.bmp'

and then it works.  But the difficulty is in batch file I get it from somewhere else, so I have to pass it as %1




Avatar of YZlat

ASKER

the part of powershell script that grabs parameter is

$args[0]
that is why I wanted to see the PS script.
You can also add this

Param(
$File)

than you can pass -file

I will have to do some testing, i have never tried to pass params through a batch loop to powershell.