Does Windows log the commands given to cmd.exe? - windows

I know that I can retrieve the commands I've given in a command window up to a certain point. cmd.exe seems to have a certain cache size and once it's exhausted the commands overflow. When I close the command window everything is deleted, right? Or wrong? Does Windows still know which commands I gave and can I retrieve them?
If I run a program in a command window and the program calls cmd.exe to run a command, can I see this command somehow?

Type doskey /?.
So doskey /history.
It is not a log but a command recaller.
echo %cmdcmdline% shows Cmd's command line. Else you can read the command line in task manager. Right click the headings in Detail tab. Choose Command Line.
This lists all variables https://winsourcecode.blogspot.com/2019/05/listenvironmentexe-list-system-user.html

Related

How start a program via command line after the previous process terminated itself?

I run a program like notepad.exe via command line.
After the process terminated itself after completion of task of the started application, I need to run another program via command line, for example winword.exe.
So I would like a behavior like a scheduler, waiting in background up to completion of first process and then initiate start of another process.
Can I achieve this in Windows? And if yes, how?
Batch file solutions
For the example Notepad.exe and Winword.exe the solution is quite simple with following batch file:
#echo off
%SystemRoot%\Notepad.exe
start Winword.exe
Windows command interpreter cmd.exe first starts Windows Notepad and halts execution of the batch file until Notepad.exe terminated itself which means the user
pressed Alt+F4 on keyboard, or
clicked on X symbol on right side of title bar of Notepad window, or
clicked in menu File on menu item Exit, or
double clicked on application symbol on left side of title bar of Notepad window, or
clicked once on application symbol on left side of title bar of Notepad window and clicked next on last application context menu item Close.
Then internal command START of cmd.exe is used to start Microsoft Word in a separate process parallel to running command process. For that reason cmd.exe continues the batch file processing immediately after executing START while Microsoft Word is running parallel and exits because there is no more command line.
The application to start must be specified usually with full path if the directory containing the executable is not included in environment variable PATH, enclosed in double quotes if the path contains a space or one of these characters &()[]{}^=;!'+,`~. Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? for details on how Windows command interpreter finds executables and scripts specified on command line or in a batch file without path (and without file extension). So best would be as second line something like:
start "" "%ProgramFiles(x86)%\Microsoft Office\Office14\Winword.exe"
This command line starts 32-bit Microsoft Word 2010 installed in standard installation directory on a computer running 64-bit Windows. The additional empty argument string "" is necessary because command START would interpret otherwise the full qualified name of application to start enclosed in double quotes as title for a new console window. So the command START would start a new command process with the title C:\Program Files (x86)\Microsoft Office\Office14\Winword.exe for the console window without specifying explicitly an empty title with "" as first argument.
Why is using just start Winword.exe working?
The directory containing Winword.exe is not included in environment variable PATH. But as long as Winword.exe is installed at all, this command line nevertheless results in starting Microsoft Word. The reason is correct registration of application Winword.exe in Windows registry during installation. For that reason the command START is capable to find out where Winword.exe is installed and execute it. For details on how this works see answer on Where is “START” searching for executables?
The three lines in batch file can be also optimized to a single line with multiple commands:
#%SystemRoot%\Notepad.exe & start Winword.exe
But this single command line can't be used directly in a command prompt window because of cmd.exe executes in this case Windows Notepad and Microsoft Word parallel.
Command line solutions
The command line solution for usage directly from within a command prompt window is:
start /wait Notepad.exe & start Winword.exe
This starts Windows Notepad in a separate process using command START with explicitly waiting for termination of Notepad.exe because of using additionally the START parameter /wait before one more START is executed to start Microsoft Word. There is no need of "" as empty title string here because of no argument string in this command line is enclosed in double quotes.
But this command line solution has one disadvantage: the command prompt window can't be used further as long as Windows Notepad is running.
So better would be starting from within current command prompt window a new command process with minimized window which first executes Windows Notepad, halts command line execution until Notepad terminates itself, then starts Microsoft Word and exits immediately after starting Winword.exe. This can be done with following command line:
start "Notepad & Winword" /min cmd.exe /C "start /wait Notepad.exe & start Winword.exe"
This command line results in starting cmd.exe as separate process, with a console window being minimized because of option /min of command START, with a console window title Notepad & Winword, which closes itself because of option /C of CMD, after first starting Notepad.exe and waiting for Notepad termination before starting Winword.exe not waiting for termination.
Well, the additional, minimized console window with title Notepad & Winword is in real of no usage for the user. Therefore better would be the command line:
start "" /B cmd.exe /C "start /wait Notepad.exe & start Winword.exe"
The additional command process is started in this case with no window (in background) because of usage of option /B of command START. An empty title is specified here as no console window is shown at all.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cmd /?
echo /?
start /?

Redirection for a command in Registry

I'm trying to add a right click option to every file so that it executes a command. Seemed simple enough at first glance.
Right click option
By going in the registry editor, specifically HKCR\*\shell (which represents the context menu options that appear for every file type, hence the "*"), I added my own key as such :
HKEY_CLASSES_ROOT
*
shell
my_right_click_option
(Default) = "Click me!"
command
(Default) = my_command
Command
After this, the clickable entry appeared in the context menu instantly.
In my case, the key is called "checkmd5" and the command key's "Default" value stores the command 'my_command' to execute when clicked.
The command is :
"C:\Quite_a_long_path\md5\md5.exe" -n "%1" | clip
The -n option (an md5.exe switch) is just to remove the name of the program that gets displayed after the hash.
The %1 will be replaced by the right clicked program's path, and the double quotes are used in case any of the paths contain some whitespaces.
Sadly, all I've managed to do is execute the command (a cmd window appears and disappears, which means that the cmd has been executed) and also, just to be sure, I checked with Process Monitor (from the SysInternals Suite) the command line being executed in the small cmd window that appears and disappears and it looks like the command I put is being executed exactly as I wanted.
Nonetheless, even after appending to the command either "| clip" (to redirect output to clipboard) or "> file.txt" (to a file) [I tried using an absolute path for file too], the clipboard doesn't update, nor the file.txt gets created with the output of the command..
I can't seem to understand how to make it work..
Any help from you windows savvy will be greatly appreciated.
Thank you again for your patience, especially after getting to the end of this long post.
Turns out the only way to use the re-directions is by adding the cmd.exe /C before the command to be executed so that the command interpreter (cmd.exe) understands the redirection and not the program to be executed (aprogram.exe).
e.g:
aprogram.exe -param0 string > C:\temp\file.txt
wouldn't work, because aprogram.exe doesn't know how to manipulate redirections.
Instead it should be :
cmd.exe /C aprogram.exe -param0 string > C:\temp\file.txt

How to launch this program from the command line?

This is the program I am trying to launch from command line (Star Guard)
I opened a new command prompt process in this directory and am trying to start the Star Guard application with command line(so I can pass in command line arguments later).
However when I do so, I don't end up launching the program but I do end up launching a new command prompt process in the same directory.(output shown below) The program launches fine when I start it normally(GUI click)
Does anyone know what the issue is? I first did the ls(OSx) equivalent in Windows to make sure I had the right file path for the executable. I then used the start command to start the program along with enclosing the executable path in quotation marks to account of the spaces.
The syntax of the start command is unique, not to mention daft. If the first argument is in quotes it is interpreted as a window title.
In this case, you don't actually need to use start at all, you could just say
"Star Guard"
or
"Star Guard.exe"
If you want to use start, perhaps because you want to specify /wait for a GUI application or because you want to launch a console application in a new window, you have to include the title argument:
start "" "Star Guard.exe"
(The title argument doesn't need to actually specify a title, it just needs to be present.)
From OS/2 Warp Help
Starts an OS/2 program in another session.
The primary use for START is to automatically start programs at system startup. The
special batch file, STARTUP.CMD, allows you to do this.
To imbed redirectional signals into the command session, enclose the command and
command inputs in quotation marks.
START
"program /K /F
title" /C /B
/N
/PGM /FS /MAX
/WIN /MIN
/PM
/DOS
command
/I command
inputs
Related Commands: RUN
Enter this command without a parameter to start an OS/2 command processor.
If you use the /WIN, /FS, or /PM parameter, your program runs in the foreground session.
If you do not use one of these parameters, you can use the /F parameter to make the
program run in the foreground session.
Make sure that you specify the correct drive and path when you use the START command to
run a batch file with the STARTUP.CMD file. Also, if you plan to redirect I/O using the
START command, enclose the command and command inputs within quotation marks.
You can use START to run full-screen applications or applications running in a window
such as Presentation Manager programs.
START determines the type of application and will run it in the appropriate window or
full-screen session. However, you have the option to override the determined default by
using the /FS, /WIN, /PM, or /I parameter.
You cannot start a batch file (.CMD) with the /PM parameter.

Keep Windows command window open

I'm running a command through the Windows shell- an existing command window (cmd.exe). When I execute the command, the window closes, even though it's a freestanding window not tied to the command.
How can I keep the window open to see the output?
You can't simply start a child cmd session because it'll share same window and if your custom tool actively closes its window (I wonder why) then it'll close your console and output will disappear.
There isn't much you can do if a program want to close console window but you can at least save its output to a file (to be inspected later with type). If you're working with that console and you don't want to close it then you can use start cmd to execute it in a new console window. Like this:
start cmd /c tool -args ^> output.txt
tool output will be available in output.txt after it finished.
It appears that the executable is closing the command window. Here is what you could try, may work. open a command shell. In the shell issue "cmd" and open another command shell. Run your executable in the newly opened command shell. You nested cmd will be exited, but you may still be able to see some of the output of your executable.

Using Batch File to Open Another Cmd Prompt and Run Cmd in that Cmd Prompt

I have another bat file that I'm running, and once in the command prompt that bat file creates, I want to run another command in that window.
Here's what I have so far:
call C:\Batch\MyBatFile.bat (this creates the new command prompt that I want to use)
C:\Program\MyProgram.exe
However, the second line is being run in the original window, instead of the new command prompt. I tried using start C:\Program\MyProgram.exe, but that just ran in a 3rd new window instead.
If it's relevant, the first line is just setting a few environment variables that I need access to and MyProgram is a visual studio 2010 project. Technically, I might be able to modify that bat to run the command, but I'd rather avoid that solution as that bat file isn't owned by me (and thus whenever it's updated I'd have to update mine as well).
Thanks in advance.
You could try to inject your program.exe into cmd created by batfile.bat by redirecting it's input stream and then sending it a command, eg. echo C:\Program\MyProgram.exe | C:\Batch\MyBatFile.bat. This assumes that batch really just sets bunch of variables and does not use commands which reset/consume input stream.
Please note that if redirected/piped this way new command window will not stay open It will maybe :-) just execute your command and then close/exit.
Create a CMD script to run both of the commands that you have shown in the question. Maybe call it RunMyProgram.cmd. The contents are just the two lines that you have:
REM Source the environment variables.
REM Any new command prompt window that is opened can be ignored
CALL C:\Batch\MyBatFile.bat
C:\Program\MyProgram.exe
If what you have stated in the comments to your question is accurate regarding MyBatFile.bat setting up the environment variables and then starting a new window, then you should be able to make use of those environment variables after MyBatFile.bat exits.
If running RunMyProgram.cmd from a command prompt still has MyProgram.exe giving the error when the environment variable is not set, or if MyProgram.exe doesn't even start to run until you close the new window that popped up, then we need to see the exact commands that MyBatFile.bat is executing.

Resources