batch file: start another batch using cmd as admin - batch-file

I need to start an application with admin rights from an user account in win 10. I use a batch file to start the command window cmd.exe with admin rights and pass a batch file to this one. Unfortunately I'm not able to minimize the cmd window (needs to stay open). It is always displayed in the middle of the screen. Is there a way to show the window minimized?
batch file: start.bat:
C:\Windows\System32\runas.exe /user:administrator /savecred "C:\Windows\System32\cmd.exe /k """C:\Program Files (x86)\test.bat""" "

Related

When opening vscode using batch file, cmd opens and doesn't close

When trying to open vscode folder using a batch file, Visual Studio opens up with that folder, but also a cmd window pops up and does not go away if you use exit command.
#echo off
start code "C:\GitHub\TestApp\testapp"
exit
VSCode opens up correctly, but also this window opens
Using VSCode 1.52.1, the only way I could start it without having a cmd window open after exiting the batch script is:
explorer.exe "%userprofile%\AppData\Local\Programs\Microsoft VS Code\Code.exe"
Note: it does not involve opening a specific local directory to work with. But maybe you can find a solution such as saving the folder as a workspace or using Ctrl+R to open recent folders. Plus, if you work only within that directory / workspace, or use it right before closing VSCode, it will be opened automatically at the next launch.
That's because you are actually invoking the batch file code.cmd which is located at [VSCodePath]\bin\code.cmd. The code.cmd file in turn invokes the actual VSCode executable code.exe
When invoking a batch file (.BAT or .CMD) using the start command, a new instance of CMD process will be created to handle the execution of the batch file, But it invokes the CMD process with the /K switch rather than /C
For example start code.cmd executes cmd /k code.cmd
It is the /K switch that causes the new cmd to remain open after finishing the execution of the batch file.
To resolve, instead of supplying the batch file directly the to the start command, execute it by an explicit CMD invokation:
#echo off
start cmd /C code "C:\GitHub\TestApp\testapp"
exit
That CMD window is associated with the VSCode instance that you just opened. Attempting to close it will terminate the application you started. (in this case, VSCode)
The start xxx xxx... command opens up a new cmd terminal to perform its action. Even though a new prompt appears, which can be used as a normal terminal itself, the VSCode process is inexorably linked to it as the parent process.
If your goal is to not launch a separate cmd window, then run:
start code /b "C:\GitHub\TestApp\testapp"
which just runs the command in the same window. The VSCode window is still inexorably bound to the current cmd window and will close if the cmd window disappears, but at least another cmd window isn't launched.
Windows doesn't have the capability to launch a program in the background from the terminal.
If all described solutions did not work for you, try making an ordinary Windows shortcut to "C:\Users\username\AppData\Local\Programs\Microsoft VS Code\Code.exe" C:\path-to-project-folder-or-file.
Then call this shortcut in your .bat or .cmd script like that (assuming shortcut name is shortcut):
#echo off
start C:\path-to-shortcut-file\shortcut

Run batch file as admin, hidden, from inno setup

I have an Inno Setup installation which successfully run a batch file when finished. The batch asks for admin rights, gets them and does things.
I've been asked to completely hide the prompt window, so I've added "Flags: runhidden" in Inno Setup and #echo off and >nul 2>nul in the batch. But the batch still flash a prompt window, because of this:
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & goto
batch & exit /b)
EXIT /b
:batch
---MyCodeHere---
I have not written this code by myself and I'm not expert about batch programming, but I'm pretty sure that these lines open a new prompt with admin permissions, and that's the prompt flashing.
Does anyone know if there's a solution to this? Maybe some flag to add to "powershell start", or another completely different way to do this task.
Thank you all in advance
Don't make the batch file ask for Administrator permissions.
Run the batch file with Administrator permissions straight away:
Run process (net.exe or sc.exe) with Administrator privileges at the end of an installation in Inno Setup
[Run]
Filename: "{app}\my.bat"; Description: "Run my batch file"; \
Flags: postinstall runascurrentuser runhidden

Run a batch script in another directory than the batch script directory

I have a batch script that I run from the Windows Task Scheduler, the advantage is that the scheduler has the option of "Start in:", this allows me to run the batch script in a totally separate directory.
In the cmd prompt the equivalent would be to open a cmd prompt window in Directory1 and type out the full Directory2/batchscript.bat.
However, I was wondering if there was a way inside my batchscript.bat to set the directory to "run" in?
Try something like this:
set "your_dir=path_to_your_directory"
pushd %cd%
cd %your_dir%
run_your_command
popd

How to stop and restart Audio Service using a batch file?

I made a batch file with the following line
net stop audiosrv & net start audiosrv
It just flashes and closes, not actually doing the command. I think I saw something about administrator privileges in the command window but it flashed too fast to tell. What is wrong?
Create a new text file.
Then, paste the following code into it.
#echo off
net stop audiosrv
pause
net start audiosrv
pause
Save the file as a bat file.
Open the bat file as admin.
Here find a script for running a batch file as an admin
#echo off
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
net stop audiosrv
pause
net start audiosrv
pause
A more comprehensive batch file approach:
#echo off
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
net stop audiosrv
pause
net stop AudioEndpointBuilder
pause
net start AudioEndpointBuilder
pause
net start audiosrv
pause
The audio service is started by Windows using local system account and therefore it is not possible to stop this service without administrator privileges as command net outputs.
The solution is clicking with right (secondary) mouse button on batch file and click with left (primary) mouse button in context menu on Run as Administrator as Magoo already suggested before.
For testing a batch file just created, it is always useful to open a command prompt window and run the batch file from within this window by entering name (with path if needed) and hitting RETURN. A shortcut for opening a command prompt window can be found in Accessories menu of Windows start menu, or the command cmd.exe is executed which also opens a console window.

How to get direct path using Batch File

I want to write simple Batch file will click on the batch file
I should go to my directory path should be D:\DS\Install
At present i am doing every 1hr go to
RUN command and typing to cmd and connect to D:\DS\Install
Instead of this I want short cut option.. :-)
If I understand your question correctly,
You want a shortcut on desktop which will open a command prompt and change directory to D:\DS\Install
This is pretty straight forward:
Go to C:\windows\system32 and copy cmd.exe to D:\DS\Install folder
Now right click on the cmd.exe in D:\DS\Install folder and send it to desktop create shortcut.
You will have a shortcut on desktop which will take you to the required folder everytime..
Just FYI... cmd.exe will always open from the directory where it is placed.
Just create a shortcut in your desktop, and when asked for the location, type this command
cmd.exe /k "cd /d "d:\ds\install""
This shortcut will call cmd to execute the cd to change the drive/folder and keep the window open to continue working with it.
You can also make a batch file called ds.bat and put it in c:\windows or any other folder that is on the path.
You can then use the WIN+R hotkey and type ds and enter.
#echo off
cd /d "d:\ds\install"
cmd /k
#echo off
cd /d "d:\ds\install"
cmd /k

Resources