How can the argument to a batch file be intrun passed onto a powershell script? - powershell

i am trying to call a powershell script through bat file such as below;
Bat file name is Script .bat and its current cuntents are below;
Powershell -sta -Windowstyle Hidden Script.Ps1
I am passing an argument to the bat file how can i pass it on to the powershell script in the bat file?
Please do let me know if any questions or confirmations needed.

In your shell script (batch file), you can write:
powershell -file script.ps1 %1
%1 will be replaced with the shell script's first command-line argument, which will be passed as the first argument to script.ps1.
Bill

Related

put a powershell script on path through powershell commands.

I have a powershell script that I want to run from cmd/ps any location by putting it in path.
What is the command that can achieve that ?
I m basically looking for a UNIX equivalent of putting your script in bashrc and thus available from anywhere to run.
echo 'export PATH=$PATH:/path/to/script' >> ~/.bashrc && source ~/.bashrc
In windows you also have the system variable PATH that's used for defining where to locate executables.
You could do the following that should be equivalent assuming you're only using Powershell:
$newPath = "c:\tmp\MyScriptPath";
[Environment]::SetEnvironmentVariable('PATH', "$($env:Path);$newPath", [EnvironmentVariableTarget]::User);
# Update the path variable in your current session; next time it's loaded directly
$env:Path = "$($env:Path);$newPath";
You can then execute your script directly in Powershell with just the name of the script.
However_ : this will not work under cmd because cmd doesn't know how to handle the ps1 script as an executable. Normally one would execute the script from cmd by calling the following:
Powershell.exe -executionpolicy remotesigned -File C:\Tmp\Script.ps1
If this is "unacceptable" for you, the easiest way is to create a bat script along with your ps1 script (same path) and add the following content :
Script.bat (Assuming you have Script.ps1 in the same folder):
#ECHO OFF
PowerShell.exe -Command "& '%~dpn0.ps1'"
PAUSE
This will create the wrapper needed to Invoke Script anywhere in your cmd as batch files can be executed from cmd

Need to call powershell script from batch file

I have a batch file which is in a folder called script. The script folder also contains folder called powershell which has a script called IE-Settings.ps1.
I want to execute the powershell script from the batch file and I am unable to give powershell script path in the command. What I tried is
call %windir%\system32\WindowsPowerShell\v1.0\powershell.exe -File "& '%~dp0IESettings\IE-Settings.ps1'"
But it doesn't recognize the path
call is for running other batch files in a way that they return to the current batch file after they terminate, and per your question the subdirectory name is powershell, not IESettings. Also, when using the parameter -File you just specify the path to the file.
powershell.exe -File "%~dp0powershell\IE-Settings.ps1"
The call operator (&) is used when running PowerShell code via the -Command parameter, e.g.:
powershell.exe -Command "& { Write-Host 'foo' }"

How can I run a powershell script from startup/autostart folder with bat file?

Are those files put into a cmd context and then just executed/typed ?
A .bat file is just run, but a powershell file can`t just be run.
How can I make it run without bat file?
You don't need a batch file (.bat or .cmd file). Just create a shortcut to %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -File whatever.ps1. You may need to use the -ExecutionPolicy parameter to allow scripts. Run powershell /? from a Cmd.exe or PowerShell prompt for more information.

Setting up a batch script to run powershell with arguments

I have a PowerShell script that takes an argument/command and executes well in PowerShell. I am looking at setting up a batch script to run following commands as is to mimic PowerShell
(-config is a function that's defined within script)
C\temp\power_t1.ps1 -config d:\temp\dirlist.txt
I wrote a batch script something like
ECHO OFF
POWERSHELL.EXE -file "C\temp\power_t1.ps1 -config d:\temp\dirlist.txt"
but this doesn't seem to be working.
Any suggestions?
Regards,
Ruben
The script file and the arguments for the script file are separate arguments for powershell.exe. If you look at powershell.exe /?, under -File, you see:
File must be the last parameter in the command, because all characters typed after the File parameter name are interpreted as the script file path followed by the script parameters.
Try:
POWERSHELL.EXE -File "C\temp\power_t1.ps1" -config d:\temp\dirlist.txt
Basically, treat everything after the -File parameter more or less as though you were writing it in PowerShell with the call operator (&).

Escaping arguments when passing %* from batch script as $args to powershell script

I have a batch script that takes any number of arguments (list of files) and executes a powershell script with the following command structure:
"%POWERSHELL%" -Command "%SCRIPT%" %*
%POWERSHELL% is the path to PowerShell.exe, and %SCRIPT% is my powershell script that interprets that receives %* as $args. The problem is that if I pass in something like the filename test$file.name, PowerShell receives test.name, presumably because $file is interpreted as an empty variable.
Is there a good way to escape each argument with single quotes or backticks from the batch script, or otherwise deal with this?
Escape $ characters before you pass %* to the PowerShell script.
set ARGS=%*
set ARGS=%ARGS:$=`$%
"%POWERSHELL%" -Command "%SCRIPT%" %ARGS%