Need to call powershell script from batch file - powershell

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' }"

Related

How to execute PowerShell script in MoveIT central?

I have a bat file include command em32\WindowsPowerShell\v1.0\powershell.EXE -NoLogo -NoProfile -Command c:\temp\GL_Format_Update.ps1. Then used command line App in MoveIT central to execute bat file. The script can't produce the output file as expected. Command can be run in CMD window successfully. It seems like MoveIT service owner can't execute PowerShell script.
I had a similar issue and found that simply putting the entire command into the CommandLineApp_AppPath was throwing an error. By breaking it up into the path to powershell and the arguments to powershell, I was able to successfully call and execute my script. My script also took 3 parameters.
Create a task with a process. Select the built-in script "Command Line App". Set the parameters as follows:
CommandLineApp_AppPath = C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.EXE
CommandLineApp_AppParms = -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "E:\PowerShell\CreateManifest.ps1 -Folder \\mdcvmsfms11u\DataTransfer\BFClientGateway\Test\Download\2129\PPfAandDP -ManifestName MS_CONTROL_ -OutputType FULL"

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

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 (&).

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

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

Powershell script not firing from batch file

I've been trying to launch a simple powershell script from a batch file. After looking online the advice is to set the policy using Set-ExecutionPolicy.
I've done this and using Get-ExecutionPolicy the policy has been changed.
However running this batch file results in an 'Open With' dialog rather than the execution of the script.
Batch:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ^&'./script.psl'
Powershell:
Write-Host "This is a message"
I have sampled on both Windows 7 and Server 2008 R2. Both have same result. What am I missing?
To run a script file from the command line, use the -file parameter of powershell.exe.
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -file './script.psl'
To run a script file from the *.cmd file , use the -file parameter of powershell.exe and double quotes:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -file "./script.ps1"
When you will use only one quote in batch file you can expect powershell error like:
Processing -File ''./build.ps1'' failed because the file does not have
a '.ps1' extension. Specify a valid Windows PowerShell script file
name, and then try again.