call bat file in new windows from powershell - windows

I want to launch a bat file in new window/independent instance from within powershell.
the bat file will take a computername from powershell
e.g.
mybat.bat $thiscomputer
Ive tried
start mybat.bat $thiscomputer
start "cmd /c" mybat.bat $thiscomputer
start /k mybat.bat $thiscomputer
start-process mybat.bat $thiscomputer
ordinarily the bat does run but stays within the powershell script.
I need the powershell script to launch the bat in anew window and loop back to the beginning.
Thanks
Confuseis

Start-Process runs console programs in a new window by default. The only problem with your syntax is that you need to use the -ArgumentList parameter to pass arguments to the program you've started. PowerShell doesn't let you simply list them after the program name.
PS> Start-Process $env:comspec -ArgumentList "mybat.bat $thiscomputer"
or, in shorter form,
PS> start mybat.bat -a $thiscomputer
-ArgumentList only takes one string, so if you have multiple arguments to pass, then you need to put them all into one string before passing to Start-Process.

Related

second bat is not executed PowerShell script

the below bat files are being called by PowerShell script, however only bat1.bat is executed the others bat2.bat and last.bat are not being called
#first bat
Start-Process "C:\bat1.bat" -Wait
#run second bat
Start-Process "C:\bat2.bat" -Wait
#run last bat
cmd.exe /c '\last.bat'
You r support is highly appreciated
Try using this:
Set-Location C:\temp
Start-Process .\bat1.bat -Wait
Write-Host "bat1.bat DONE"
Start-Process .\bat2.bat -Wait
Write-Host "bat2.bat DONE"
cmd /c .\last.bat
Write-Host "last.bat DONE"
I made the following .bat files:
bat1.bat
#ECHO OFF
ECHO bat1
PAUSE
bat2.bat
#ECHO OFF
ECHO bat2
PAUSE
final.bat
#ECHO OFF
ECHO final
When the PS script was ran, all 3 ran as one would expect.
Putting the Write-Host will show in the console if the previous process finished.
Edit:
I didn't give much explanation before, sorry.
Your issue is that one of the .bat files is not finishing.
The syntax you had should have run bat1.bat and bat2.bat. The "cmd.exe /c "\last.bat" would fail. You need the "c:\last.bat" if you aren't going to set the path to c:. If you did that, you would want ".\last.bat"
To make sure that your script would work, I made 3 basic .bat files whose functions were solely to log a string associated with them. Doing this guaranteed that if anything went wrong, it was with the PS code and not the .bat code.
After verifying your script would run, I made the one I provided. Using Set-location isn't necessary, but I think it makes the code look cleaner. The Write-Host after each process is logging in the PS console that the script is moving on to the next process.
Why not try
CALL "C:\bat1.bat"
CALL "C:\bat2.bat"
\last.bat
and avoid Powersmell altogether?

How can I set location to current working folder?

I have a powershell script located in "register" folder which I want to use to execute and exe file in "app" subfolder also located in "register" folder dynamically. Each time I run the script I get path not found error but I want the powershell to maintain the current working folder location so that it can work each time I copy to another drive.
Below is the review of my code
"cmd.exe /c PowerShell.exe -windowstyle hidden Start-Process '.\Register\App\record.exe'"
Thanks.
It's not clear where your command is being executed, but I believe the surrounding double quotes are causing everything within to be treated as the executable file name instead of just cmd.exe.
If I open Command Prompt at %UserProfile% and copy %SystemRoot%\system32\calc.exe to %UserProfile%\Register\App\record.exe, then the following work for me:
Command Prompt without surrounding double quotes: cmd.exe /c PowerShell.exe -windowstyle hidden Start-Process '.\Register\App\record.exe'
PowerShell without surrounding double quotes: PowerShell.exe -windowstyle hidden Start-Process '.\Register\App\record.exe'
Both of these...
Command Prompt with surrounding double quotes: "cmd.exe /c PowerShell.exe -windowstyle hidden Start-Process '.\Register\App\record.exe'"
PowerShell with surrounding double quotes: "PowerShell.exe -windowstyle hidden Start-Process '.\Register\App\record.exe'"
...result in...
The system cannot find the path specified.
Also, just in case it's not clear, you are attempting to run cmd.exe to run powershell.exe to run record.exe. At the very least, the cmd.exe /c is unnecessary; just run PowerShell directly.

Using a cmd command in powershell script?

I am trying to make a powershell script that automatically sets up this program and requires to use a cmd command to run do it.
I know that the cmd command work and I tried cmd.exe.
cmd.exe /c "java -jar fitnesse-standalone.jar -p 9090"
Error Message:
cmd.exe : The term 'cmd.exe' is not recognized
You can use start-process
Start-Process -FilePath "cmd.exe" -ArgumentList '/c "java -jar fitnesse-standalone.jar -p 9090"'
The /c and everything following it is just part of the one set of parameters being passed to cmd. If you want powershell to wait for the the Java app to close, add the -wait parameter.
There's also no real need to use CMD at all in this case (since your giving it /c to exit immediately anyway), you can call java directly:
Start-Process -FilePath "java.exe" -ArgumentList '-jar fitnesse-standalone.jar -p 9090'
and it should be the same.
Note: you'll need Java in your path or the current working directory, and fitnesse-standalone.jar in the current directory for either of these to work.

In powershell after changing to cmd unable to call a bat file through a batch script file

I created a bat file to setup my workspace by changing the directory to the workspace directory and calling the setupEnv.bat file. But while I'm executing the below bat file in PowerShell, the instructions after cmd are not executing.
I need to call the setupEnv.bat file in cmd. If I remove the cmd it will work fine. But I want call the setupEnv.bat on cmd not in PowerShell.
D:
cd D:\WorkSpace\
cmd
call setupEnv.bat
echo "Setup Completed"
After calling the setupEnv.bat and calling cmd, will it keep all the environment variable setup in the PowerShell ?
This article addresses your exact scenario:
Windows IT Pro - Take Charge of Environment Variables in PowerShell
The reason the variables disappear is that a .bat or .cmd runs in a separate cmd.exe process (when the process terminates, you lose the variables).
The article presents a PowerShell function you can use to run a .bat or .cmd script and retain the environment variables it sets:
# Invokes a Cmd.exe shell script and updates the environment.
function Invoke-CmdScript {
param(
[String] $scriptName
)
$cmdLine = """$scriptName"" $args & set"
& $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
Select-String '^([^=]*)=(.*)$' |
ForEach-Object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
Set-Item Env:$varName $varValue
}
}
The article also has a couple of functions for setting and restoring environment variable values in PowerShell.
try Something like this into your PowerShell script:
& start "C:\Temp\test.bat"
After calling the setupEnv.bat and calling cmd, will it keep all the environment variable setup in the PowerShell?
No. Any legacy commands invoked from PowerShell will run in a separate (child) process.
Proof:
wmic process where "name='powershell.exe' or name='cmd.exe'" get CommandLine, name, ParentProcessId, ProcessId /Value
Add above line to your batch-file, e.g. to setupEnv.bat and call it from powershell
either directly, e.g. D:\bat\setupEnv.bat,
or using & call operator & D:\bat\setupEnv.bat,
or . dot sourced & D:\bat\setupEnv.bat,
or modify all above methods calling cmd as
cmd /D /C D:\bat\setupEnv.bat, or
& cmd /D /C D:\bat\setupEnv.bat, or
. cmd /D /C D:\bat\setupEnv.bat.
Result is always the same or at least very alike:
PS D:\PShell> D:\bat\setupEnv.bat
"Setup Completed"
CommandLine="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Name=powershell.exe
ParentProcessId=4280
ProcessId=6396
CommandLine=C:\Windows\system32\cmd.exe /c ""D:\bat\setupEnv.bat""
Name=cmd.exe
ParentProcessId=6396
ProcessId=4116
Paraphrased from this Foredecker's answer to similar question:
While a child process can inherit the current environment variables,
working directory etc. from parent one, there is no supported way for
a child process to reach back to the parent process and change the
parent's environment.
Solution: call Powershell from cmd/batch script rather than vice versa

Run a .cmd file through PowerShell

I am trying to run a .cmd file on a remote server with PowerShell.
In my .ps1 script I have tried this:
C:\MyDirectory\MyCommand.cmd
It results in this error:
C:\MyDirectory\MyCommand.cmd is not recognized as the name of a cmdlet,
function, script file, or operable program.
And this
Invoke-Command C:\MyDirectory\MyCommand.cmd
results in this error:
Invoke-Command : Parameter set cannot be resolved using the specified named
parameters.
I do not need to pass any parameters to the PowerShell script. What is the correct syntax that I am looking for?
Invoke-Item will look up the default handler for the file type and tell it to run it.
It's basically the same as double-clicking the file in Explorer, or using start.exe.
Go to C:\MyDirectory and try this:
.\MyCommand.cmd
Try invoking cmd /c C:\MyDirectory\MyCommand.cmd – that should work.
To run or convert batch files to PowerShell (particularly if you wish to sign all your scheduled task scripts with a certificate) I simply create a PowerShell script, for example, deletefolders.ps1.
Input the following into the script:
cmd.exe /c "rd /s /q C:\#TEMP\test1"
cmd.exe /c "rd /s /q C:\#TEMP\test2"
cmd.exe /c "rd /s /q C:\#TEMP\test3"
*Each command needs to be put on a new line, calling cmd.exe again.
This script can now be signed and run from PowerShell outputting the commands to command prompt / cmd directly.
It is a much safer way than running batch files!
First you can reach till that folder:
cd 'C:\MyDirectory'
and then use:
./MyCommand.cmd

Resources