How to auto close batch file after executing powershell script - powershell

I have a batch file to run a powershell script but I am unable to find a command to auto close the batch file after running it.
This is my batch file code:
powershell -ExecutionPolicy Bypass -File C1.ps1
I tried the following code to auto close batch file but it wouldn't.
timeout 2 >nul
exit
move nul 2>&0

Related

I'm trying to run a powershell script from a batch file batch file but it appears to terminate the batch file

I have a PowerShell script that I'm trying to call in a batch file. The Powershell script renews some credentials used by a subsequent .exe file, which expire after an hour. I would then like to kill the PowerShell script, restart the PowerShell script, and restart the .exe. Here's a simple example that reproduces my problem:
Test.bat
::#echo off
title Started at %time%
pause
Call CallPSWrapper.bat
::Never hits this pause
pause
echo call the .exe here
CallPSWrapper.bat:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -ExecutionPolicy bypass -NoProfile -File .\MyPowerShell.ps1
MyPowerShell.ps1:
Write-Output "Waiting"
Start-Sleep -Seconds 30
I initially tried calling MyPowerShell.ps1 from test.bat, but tried changing it to being called by an intermediate batch file in the hope that PowerShell would only kill CallPSWrapper.bat.

While executing PowerShell through batch script I want to store execution status log file for that PowerShell

I'm trying to execute PowerShell inside batch script. It is executing perfectly, but I'm trying to create log file for that PowerShell execution status. It is creating empty file.
I want to store data of PowerShell execution status.
Batch file (Test.bat):
powershell.exe -ExecutionPolicy RemoteSigned -File
C:\Users\rani\Documents\String\Unt1.ps1 "%user%" "%password%"
"%path%" >>C:\Users\rani\Documents\B2_Move.txt
It is not storing the PowerShell execution data in log file.

How to close cmd window after a batch job run

Hi I have written a batch file which will run through a scheduler. The batch file job is to truncate the data from a file. But the problem I am facing here is after the batch file ran the cmd window is not closing automatically. That is why next time scheduler won't able to run the batch job throwing an error" process can not access the file".
And also scheduler status is always showing "Running" .It should be " Ready" state after completion of job. but the here job is not completing as the cmd window is opened.
Can anyone help me out how to exit from cmd window?
#echo off
powershell.exe -noexit -Command "Clear-Content -Force E:\Logs\pgbouncer.log";
exit %ERRORLEVEL%
-NoExit
Doesn't exit after running startup commands.
Powershell.exe Documentation
You have -noexit switch enabled. Use:
powershell.exe -Command "Clear-Content -Force E:\Logs\pgbouncer.log";

How to fetch an already opened cmd window and run commands in it from a batch file?

There is a bat file in which a command (nqcmd) is executed. This command only executes, when we open the batch file as an administrator. Otherwise, an error comes.
I tried to run the batch file as administrator from powershell, like this,
powershell.exe -Command "start-process -filepath C:\foo.cmd -verb runas"
It opens two cmd windows - a normal cmd window, in which the batch file executes and an error comes:
'nqcmd is not recognized as an internal or external command, operable program or batch file',
and an administrator's cmd window.
But, in this window, that batch file does not executes. We have to type the batch file's name again and then it executes.
I wanted to ask that, in a separate batch file, can we take that instance of administrator's cmd window and then run commands in it, without manually typing in that window?
Thank you.

Batch File - Commands not executing after Powershell command

I have written simple batch script to replace some text in a file, when executed via command line but when those commands are copied to .bat file execution stops after powershell command. Any idea how to execute powershell commands in batch file?
arco444's answer worked for me in comments:
You just call powershell.exe with no parameters so it starts an interactive session. If you were to type exit your batch script would continue. Use the -command switch to execute whatever it is you want to. And please edit your question and include the code there. Also You need to put the powershell command in quotes. i.e. powershell -command "get-content file.cs"