close powershell window from batchscript - windows

I want to close powershell window after my batch script completes execution.
This is my batch script. I tried exit command but it is closing only command prompt.
#ECHO OFF
setlocal EnableDelayedExpansion
start PowerShell.exe -Command help
powershell.exe -command exit
I want to close powershell window from my batchscript. Which command I am missing?

Kindly use below code in your CMD prompt to kill the existing PowerShell window.
taskkill /IM "powershell.exe" /F

Multiple commands on the same line are separated by a SEMICOLON ;.
powershell -NoLogo -NoProfile -Command "help; exit"
Does this do what you want?

Related

How can I change directory directly from the windows run prompt?

How can I change the current directory from the the windows run prompt when we initialize powershell as powershell. For eg: powershell --someArgs
You can use Set-Location or Cd
Powershell.exe -noexit -Command "CD 'C:\Test\'"
Powershell.exe -noexit -Command "Set-Location 'C:\Test\'"
Launch cmd.exe and use the builtin start command's /d flag to launch powershell.exe with a specific working directory:
cmd /c start /d "C:\working\directory\goes\here" powershell.exe
This will set the working directory of the powershell.exe process to C:\working\directory\goes\here, in turn causing PowerShell's $PWD to change to that directory

Keep batch script running after launching powershell within

I am using a batch script to automate a lot of tasks I have to run on a computer.
In the batch file I have a menu making you choose between each task separately or do everything.
When I choose to do everything, after launching the first .ps1 it just kills my cmd window.
I found some way to keep the window open but the script doesn't continue...
Also I created a "master file" to call all the ".ps1" and tried those cmds: call, /wait
The call feature works and they all start. So I don't understand why they would do the same in my script.
The cmd lines bellow are not bellow each other in my script because I want to be able to call them separately.
Part of the script I would like to run properly:
echo Windows Layout
start /wait PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dp0Customisation\Export_Import_Layout.ps1""' -Verb RunAs}"
echo Remove Unwanted Apps
start /wait PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dp0Clean\RemoveAppx.ps1""' -Verb RunAs}"
wmic product where name="WinZip 22.5" call uninstall
ping -n 6 localhost >nul
Taskkill /IM /F "MicrosoftEdge.exe"
Taskkill /IM /F "MicrosoftEdgeCP.exe"
Taskkill /IM /F "MicrosoftEdgeSH.exe"
%~dp0Clean\MCPR.exe
The master.bat looks like this :
call PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dp0InstallChocoAndApps.ps1""' -Verb RunAs}"
call PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dp0Clean\RemoveAppx.ps1""' -Verb RunAs}"
Any hint would be appriciated.
Please keep in mind I am not a script specialist.
Thanks
Ben
Managed to solve my issue by adding start "" /wait instead of start /wait

How to run PowerShell in CMD

I'm trying to run a PowerShell script inside cmd command line. Someone gave me an example and it worked:
powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"
But the problem is my PowerShell script has input parameters, so I tried, but it doesn't work:
powershell.exe -noexit "& 'D:\Work\SQLExecutor.ps1 -gettedServerName "MY-PC" ' "
The error is:
The term 'D:\Work\SQLExecutor.ps1 -gettedServerName "MY-PC" ' is not recognized as the name of a cmdlet, function,
How can I fix this problem?
You need to separate the arguments from the file path:
powershell.exe -noexit "& 'D:\Work\SQLExecutor.ps1 ' -gettedServerName 'MY-PC'"
Another option that may ease the syntax using the File parameter and positional parameters:
powershell.exe -noexit -file "D:\Work\SQLExecutor.ps1" "MY-PC"
I'd like to add the following to Shay Levy's correct answer:
You can make your life easier if you create a little batch script run.cmd to launch your powershell script:
run.cmd
#echo off & setlocal
set batchPath=%~dp0
powershell.exe -noexit -file "%batchPath%SQLExecutor.ps1" "MY-PC"
Put it in the same path as SQLExecutor.ps1 and from now on you can run it by simply double-clicking on run.cmd.
Note:
If you require command line arguments inside the run.cmd batch, simply pass them as %1 ... %9 (or use %* to pass all parameters) to the powershell script, i.e.
powershell.exe -noexit -file "%batchPath%SQLExecutor.ps1" %*
The variable batchPath contains the executing path of the batch file itself (this is what the expression %~dp0 is used for). So you just put the powershell script in the same path as the calling batch file.
Try just:
powershell.exe -noexit D:\Work\SQLExecutor.ps1 -gettedServerName "MY-PC"

Batch script to close all open Command Prompt windows

I have a .cmd file which I call to open multiple instances of Command Prompt via:
launcher.cmd -fs
launcher.cmd -tds
launcher.cmd -fsd
Each command open a new command prompt.
So what I want to do is create a batch file to automatically close all the opened Command Prompt instead of manually doing it.
Be carefull: you might kill more processes than you want:
taskkill /IM cmd.exe
You can add extra filters:
taskkill /IM cmd.exe /FI "WINDOWTITLE eq launcher*"
use
tasklist /FI "imagename eq cmd.exe " /V
to get a glimpse of what cmd.exe processes will be taskkill-ed
You could add the /F parameter to force the process to close but I would only use that if the process doesn't respond to a normal request.
Just a little note why accepted answer from Rene may not work. I was starting my apps from cmd file like
start "" my.exe -my -args
where my.exe was a console app and it was looking like cmd window I wanted to kill, but process name was not cmd.exe (!) and I had to use command like
taskkill /IM my.exe
So in some cases it worth to check the real process name, for example in the windows task manager.
TASKKILL /F /IM cmd.exe /T
good solution

Substitute windows cmd

I found PowerCmd. And would like to substiture cmd - as default IDE for execution of bat files.
But simple replacin of comspec do nothing.
I cann't also rename cmd.exe in %SYSTEM32%.
Is possible to substiture or not?
Thanks.
I am giving answer for Powershell, should be similar for anything else that you want to use:
Use Regedit and goto
HKEY_CLASSES_ROOT\batfile\shell\open\command
Set the default value to
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit "%1" %*
Now when you double click on any batch file, it should run on powershell. -noexit gets to the powershell prompt after completion of the batch file.

Resources