Solved

Pass a parameter to a powershell file from batch file

Posted on 2011-02-24
8
1,334 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:YZlat
  • 4
  • 4
8 Comments
 
LVL 27

Expert Comment

by:KenMcF
ID: 34970411
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



0
 
LVL 35

Author Comment

by:YZlat
ID: 34970456
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


0
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 total points
ID: 34970564
Can you post the script?
it depends on your env path, you may need the full path to powershell.exe
the default is
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
then depending on what parametes the script needs you can put them after the ps1 file.

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



0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 35

Author Comment

by:YZlat
ID: 34970646
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
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34970730
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.
0
 
LVL 35

Author Comment

by:YZlat
ID: 34970781
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




0
 
LVL 35

Author Comment

by:YZlat
ID: 34970888
the part of powershell script that grabs parameter is

$args[0]
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34970957
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.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question