[SOLVED] How to Keep Command Prompt Programs Running?

double clicking a script file doesn't execute them correctly... never has. That's how you run uninteractive scripts.

These are INTERACTIVE because you need the output, therefore you type them in manually to the command box.

Pause at the end of an uninteractive script does help in some cases... but if you have SFC just pausing in the middle for no apparent reason you have larger problems.

Also, you'd better get used to powershell, because it's all you're going to have soon enough.

Always run CMD scripts by right-click > Run as administrator. Never double click them.

Oooo, I supposed &pause will instantly go along with sfc /scannow.

I always figured that it did sfc /scannow, and then did &pause.

What about using &pause | sfc /scannow ?

I can do echo y | chkdsk /f /r C: to answer the prompt on if I want to lock the drive and do the task after restarting.
 
@Appletax No... pause in cmd.exe simply pauses when it's called. There's no & involved though... that's a different mess and part of the problem.

I never run scripts from a double click or a right click, I always call them from a command box. The why's are many... and I've seen the behavior you describe. The only way I know to avoid it is to open the admin command prompt and call the script from in there. No more double clicking anything, no more right clicking anything.

You're in command prompt land now, stop hobbling yourself with the GUI.
 
If you are running commands such as SFC or DISM in batch scripts, they should be invoked to run asynchronously with the START command in most cases and with their output redirected to files that serve as your logs.
 
If I'm using .BAT files I never even open a Command Prompt window.

Your practices are, how shall I say this delicately, atypical.

The .bat automatically opens Command Prompt. I don't open it. I don't have it running silently in the background.

Yea, atypical is the &pause.

Long ago, I didn't like CMD closing, so I found that method for keeping it open post process.

I cannot see the results of DISM or SFC if the window closes.

I like to see if they found any issues. I won't know unless I use &pause or output (log) the results to a text file.
 
I cannot see the results of DISM or SFC if the window closes.

Then, for the love of heaven, do what many of us have already said: Open a Command Prompt window as Admin and paste the commands in.

Run the things in the foreground in a Command Prompt window if you want to see the output and have the window remain. That's actually how these commands were intended to be run.

I haven't hand typed one of these commands in years. I have my "greatest hits" in a text file from which I copy them and paste them in a command prompt window whenever I need to run them.

I also have lots of BAT files, but not for utilities such as DISM or SFC.
 
.bat is dead, and is basically interchangeable with .cmd now. Command.com processed .bat files, it's long gone. cmd.exe is on the way out the door and does both .bat and .cmd now. Powershell does .ps1, and in Win11 also .cmd...

Fortunately for simple things .bat and .cmd script structures are the same, ps1 is... different. No choice but to learn it, eventually it'll be the only option.
 
@Appletax No... pause in cmd.exe simply pauses when it's called. There's no & involved though... that's a different mess and part of the problem.

I never run scripts from a double click or a right click, I always call them from a command box. The why's are many... and I've seen the behavior you describe. The only way I know to avoid it is to open the admin command prompt and call the script from in there. No more double clicking anything, no more right clicking anything.

You're in command prompt land now, stop hobbling yourself with the GUI.

Ugh, more work to run the script.

I would do something like:

Open CMD admin > cd E:\Scripts > SFC.bat > Enter.

I tried it and it freaked out. Kept repeatedly displaying the same thing:

Ugh.jpg

(In this instance, I ran it from the Desktop, which is stored in their OneDrive).

Right-click > Run as admin is so much easier lol.
 
.bat is dead, and is basically interchangeable with .cmd now. Command.com processed .bat files, it's long gone. cmd.exe is on the way out the door and does both .bat and .cmd now. Powershell does .ps1, and in Win11 also .cmd...

Fortunately for simple things .bat and .cmd script structures are the same, ps1 is... different. No choice but to learn it, eventually it'll be the only option.

I am in need of a PowerShell replacement for my three favorite Command Prompt processes.

Check Disk
DISM RestoreHealth
SFC Scannow

For the life of me, I cannot figure out how to do Check Disk's replacement, Repair-Volume.

And, I don't think Repair-Volume will rescue data from bad sectors like chkdsk's /r option does.

Repair-Volume apparently only repairs the file system.
 
If you have bad sectors, the data is lost... that's an old idea and process. Corrupt data is exactly that, there is no "recovery" of which you speak... not really anyway. NTFS does all that for you! If NTFS has lost it, it's GONE!

As for your first question... it has me seriously questioning things I shouldn't be questioning...

Are you honestly saying you're confused about running Repair-Volume -DriveLetter C -Scan ? The details of how to use the command pop up by simply running it without arguments... the EXACT SAME WAY chkdsk does...

DISM and SFC both work identically from powershell as they do from cmd.exe as they are system utilities.

The only thing that Repair-Volume cannot do is schedule boot time scans, however... it is VASTLY more time and resource efficient relative to chkdsk and doesn't damage SSDs to boot. It's a direct replacement, and yes we should ALL be using it instead of chkdsk... but chkdsk is so much easier to type in the relevant stuff to make it run I still use it out of reflex too. I despise powershell due to how verbose it is, we have tab completion... but there are limits. The language makes me want to cry, but it's how you command Windows to do things in 2021. It's also how you manage servers... all of Azure / M365... everything Microsoft does. So I have to use it and know it.

And that repeating error? It means your script is flat written incorrectly and I'm left wondering how it ever worked.
 
If you have bad sectors, the data is lost... that's an old idea and process. Corrupt data is exactly that, there is no "recovery" of which you speak... not really anyway. NTFS does all that for you! If NTFS has lost it, it's GONE!

As for your first question... it has me seriously questioning things I shouldn't be questioning...

Are you honestly saying you're confused about running Repair-Volume -DriveLetter C -Scan ? The details of how to use the command pop up by simply running it without arguments... the EXACT SAME WAY chkdsk does...

DISM and SFC both work identically from powershell as they do from cmd.exe as they are system utilities.

The only thing that Repair-Volume cannot do is schedule boot time scans, however... it is VASTLY more time and resource efficient relative to chkdsk and doesn't damage SSDs to boot. It's a direct replacement, and yes we should ALL be using it instead of chkdsk... but chkdsk is so much easier to type in the relevant stuff to make it run I still use it out of reflex too. I despise powershell due to how verbose it is, we have tab completion... but there are limits. The language makes me want to cry, but it's how you command Windows to do things in 2021. It's also how you manage servers... all of Azure / M365... everything Microsoft does. So I have to use it and know it.

And that repeating error? It means your script is flat written incorrectly and I'm left wondering how it ever worked.
Wow, good to know!

I have a very fast gaming laptop with NVME SSD.

Open PowerShell as admin > Repair-Volume -DriveLetter C -OfflineScanAndFix > Enter.

It completes instantly with NoErrorsFound. It does not require the system to be reboot like chkdsk does.

It can search my 158GB of data and find 0 issues in milliseconds?

I run chkdsk /f /r C: and it takes a few minutes - it's actually doing work!

Another quibble - have to enable PowerShell to run scripts before I can use this as a script. So, I guess I may as well just have a .txt file and copy/paste the commands into PowerShell instead.

Edit:

I restart my laptop and it processes Repair-Volume in like 5 seconds. Seems way too fast!

Also, does it make sense to recover data from bad sectors on an SSDs? I just realized that SSDs may not have sectors like an HDD has.

1.jpg

2.jpg

I run Repair-Volume on my 8TB external HDD and it is super fast, too.

If I run chkdsk /f /r on it, it takes DAYS to process.
 
Last edited:
Just open powershell and call the ps1, it will run.

And just because something takes longer, doesn't mean it's "doing actual work" it just means it's taking longer.

You need to unlearn so much it's objectively terrifying.
 
You need to unlearn so much it's objectively terrifying.

Seconded. I have seen more unfounded thinking in this very topic, and in a venue where I thought virtually all participants would know better, than I have in many end user forums.

If ever there were an example of where the advice, "Forget everything you think you know and start over again, seeking guidance from those who do know at every turn," applies . . .
 
In Linux they have a process called backgrounding. It's a switch used with the command line to cause the process to launch, run in the background. Then you can just close the CLI window. The M$ version is below. You can also pipe it to a file so you can run the command in verbose and then check the output when compleated.

START /B program > somefile.txt

from - https://superuser.com/questions/198525/how-can-i-execute-a-windows-command-line-in-background
And the Powershell version is Start-Process. This is essential in Powershell, because if you have to call an external app with arguments... you MUST USE start-process, if you don't you'll get errors at random. So it's better to get into the habit of using it.

eg:
powercfg.exe /setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c

becomes

Start-Process -FilePath 'powercfg.exe' -ArgumentList '/setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c'
 
powercfg.exe /setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c

becomes

Start-Process -FilePath 'powercfg.exe' -ArgumentList '/setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c'

Hence the reason I really dislike PowerShell. It makes what was once simple, and pretty much straightforward and compact, insanely long and messy. Who in the hell ever came up with the command line flags that all seem to be a minimum of 5 characters and go to much longer, with embedded hyphens, with no abbreviations?!!

I'm learning it, but I dislike it. The one and only blessing is that they have aliased-in a very great many of the command prompt commands we all know and love.
 
@britechguy It's not a scripting language, it's a programming language and if you're writing it outside of an IDE with tab completion you're doing it wrong.

Inside the correct scope, the code is produces is more verbose, but MUCH easier to read and less prone to error.

The problem is... well... as you no doubt well know... WE TYPE THIS JUNK IN MANUALLY A LOT!

And creating files full of code snippets you have to copy / paste to keep my hair in my head is not only inefficient, but infuriating.

But I have no idea how CMD.EXE could be expanded to do all the things Powershell can do, and if you tried to abbreviate everything, the code would be illegible... because the same script can be manipulating stuff on the local machine, a domain controller, AND an M365 tenant at the same time. That's BONKERS, but what powershell was designed for.
 
@britechguy It's not a scripting language, it's a programming language and if you're writing it outside of an IDE with tab completion you're doing it wrong.

Inside the correct scope, the code is produces is more verbose, but MUCH easier to read and less prone to error.

The problem is... well... as you no doubt well know... WE TYPE THIS JUNK IN MANUALLY A LOT!

And creating files full of code snippets you have to copy / paste to keep my hair in my head is not only inefficient, but infuriating.

But I have no idea how CMD.EXE could be expanded to do all the things Powershell can do, and if you tried to abbreviate everything, the code would be illegible... because the same script can be manipulating stuff on the local machine, a domain controller, AND an M365 tenant at the same time. That's BONKERS, but what powershell was designed for.

Well, no one has ever ranted as much, or as consistently, as I have about the need to comment code. But I don't find what PowerShell does to be anything even kinda-sorta like "self documenting" due to it verbosity. I don't, at all, find it much easier to read or less error prone. The way I'll describe writing PowerShell code is that it's akin to what reading Henry James is like for me: By the time I get to the end of the sentence I can't remember a big part of what came at the beginning or middle. It's just plain messy.

I learned, many years ago and the very hard way, that being "too clever by half" when you've written code (in that case, in C, and insanely compact rather than verbose) and not commenting what you did will come back to bite you in the ass when you cannot unravel it later when you revisit what you, yourself, wrote.

One of the things I wanted/needed to do for my blind and VI clients was to enable sound in Windows Safe Mode, which I have never understood why it isn't to begin with. Here's one of the .BAT files (I'm not going to argue file extensions here, I was used to .BAT and used it) for enabling that:
-----------------------
@echo off

REM Get the path to the current folder in which this is running. This will include the terminal backslash on the path. See the
REM documentation for the FOR command near the end for details on the series of environment variables using tilde and letter(s)
SET CURDIR=%~dp0
SET LOGFILE=%CURDIR%GetSafeModeSound.log


REM Check to see if the keys needed already exist. Each will be checked in succession, and if it doesn't exist then
REM attempt to create it and set its value.


REM The key {4D36E96C-E325-11CE-BFC1-08002BE10318} is the first that must be present.

REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318} > %LOGFILE% 2>&1 && (
ECHO.
ECHO The master key to allow sound in Safe Mode is already present on your system. Will attempt to set ownership, permissions, and value to allow sound in safe mode.
ECHO If that is successful, will continue on to check for presence or absence of other related keys.
) || (
ECHO.
ECHO The QUERY on the master sound key, {4D36E96C-E325-11CE-BFC1-08002BE10318}, indicates it is absent.
ECHO Now attempting to create it and set its value.
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318} ^
/ve /t REG_SZ /d "Sound, video and game controllers" /f >> %LOGFILE% 2>&1 && (
ECHO.
ECHO {4D36E96C-E325-11CE-BFC1-08002BE10318} key created and value set successfully.
ECHO Now proceeding to check for presence or absence of other related keys.
) || (
ECHO.
ECHO Error creating necessary key and setting value. Sound will not be available in Safe Mode. Aborting.
PAUSE
EXIT 1
)
)

REM Find out whether we're running on a 64-bit or 32-bit system, and choose SetACL executable accordingly
REM The environment variable %PROGRAMFILES(x86)% is only defined on 64-bit systems, otherwise it's a 32-bit system
IF DEFINED PROGRAMFILES(x86) (
SET EACLCMD="%CURDIR%64-bit\SetACL.exe"
) ELSE (
SET EACLCMD="%CURDIR%32-bit\SetACL.exe"
)

REM Use the appropriate SetACL utility from https://helgeklein.com to reset the owner of the key that needs to be deleted and recreated
REM so that it is owned by the Administrators group (represented by the SID S-1-5-32-544, which is the same no matter what
REM language is being used on the system being tweaked).



REM Stick a blank line in before whatever will be written out by the SetACL command
ECHO.
ECHO Setting ownership on {4D36E96C-E325-11CE-BFC1-08002BE10318} registry key to Administrators Group
%EACLCMD% ^
-on "HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318}" ^
-ot reg -actn setowner -ownr "n:S-1-5-32-544"
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO SetACL returned error code: %ERRORLEVEL%
ECHO Registry key ownership could not be set to Administrator Group. Aborting after message is presented.
PAUSE
EXIT 1
)

REM Now use SetACL to grant Full Control to the Administrators group on the key so that it can be deleted and re-created
REM Stick a blank line in before whatever will be written out by the SetACL command
ECHO.
ECHO Setting Full Control on {4D36E96C-E325-11CE-BFC1-08002BE10318} registry key to Administrators Group
%EACLCMD% ^
-on "HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318}" ^
-ot reg -actn ace -ace "n:S-1-5-32-544;p:full"
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO SetACL returned error code: %ERRORLEVEL%
ECHO Registry key permission could not be set to Full Control for Administrator Group. Aborting after message is presented.
PAUSE
EXIT 1
)


REM Stick a blank line in before whatever will be written out by the SetACL command
ECHO.
ECHO Deleting {4D36E96C-E325-11CE-BFC1-08002BE10318} registry key prior to recreating it with the needed value

REG DELETE HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318} /va /f >> %LOGFILE% 2>&1
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO Registry key Delete failed, returning error %ERRORLEVEL%. Aborting after message is presented.
PAUSE
EXIT 1
)


REM Add the key back, with the (Default) value set to be "Sound, video and game controllers"
REM This will allow sound to be on when booting into Safe Mode on the computer

ECHO.
ECHO Creating {4D36E96C-E325-11CE-BFC1-08002BE10318} registry key with value of "Sound, video and game controllers"
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318} ^
/ve /t REG_SZ /d "Sound, video and game controllers" /f >> %LOGFILE% 2>&1
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO Registry key Add failed, returning error %ERRORLEVEL%. Aborting after message is presented.
PAUSE
EXIT 1
)

REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\AudioEndpointBuilder >> %LOGFILE% 2>&1 && (
ECHO.
ECHO The AudioEndpointBuilder key is already present on your system.
ECHO Now proceeding to check for presence or absence of other related keys.
) || (
ECHO.
ECHO The QUERY on the AudioEndpointBuilder key indicates it is absent on your system.
ECHO Now attempting to create it and set its value.
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\AudioEndpointBuilder ^
/ve /t REG_SZ /d "Service" /f >> %LOGFILE% 2>&1 && (
ECHO.
ECHO AudioEndpointBuilder key created and value set successfully.
ECHO Now proceeding to check for presence or absence of other related keys.
) || (
ECHO.
ECHO Error creating AudioEndpointBuilder key and setting value. Sound will not be available in Safe Mode. Aborting.
PAUSE
EXIT 1
)
)

REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\AudioSrv >> %LOGFILE% 2>&1 && (
ECHO.
ECHO The AudioSrv key is already present on your system.
ECHO Now proceeding to check for presence or absence of other related keys.
) || (
ECHO.
ECHO The QUERY on the AudioSrv key indicates it is absent on your system.
ECHO Now attempting to create it and set its value.
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\AudioSrv ^
/ve /t REG_SZ /d "Service" /f >> %LOGFILE% 2>&1 && (
ECHO.
ECHO AudioSrv key created and value set successfully.
ECHO Now proceeding to check for presence or absence of MMCSS key, the final one needed.
) || (
ECHO.
ECHO Error creating AudioSrv key and setting value. Sound will not be available in Safe Mode. Aborting.
PAUSE
EXIT 1
)
)

REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\MMCSS >> %LOGFILE% 2>&1 && (
ECHO.
ECHO The MMCSS key is already present on your system.
ECHO All key checks complete. Sound should be available when booting into Safe Mode with Networking.
) || (
ECHO.
ECHO The QUERY on the MMCSS key indicates it is absent on your system.
ECHO Now attempting to create it and set its value.
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\MMCSS ^
/ve /t REG_SZ /d "Service" /f >> %LOGFILE% 2>&1 && (
ECHO.
ECHO MMCSS key created and value set successfully.
ECHO All key checks complete. Sound should be available when booting into Safe Mode with Networking.
) || (
ECHO.
ECHO Error creating MMCSS key and setting value. Sound will not be available in Safe Mode. Aborting.
PAUSE
EXIT 1
)
)


ECHO.
ECHO Registry configured successfully for sound in Safe Mode with Networking.
PAUSE
EXIT 0
------------------------

Whether one considers it elegant/good/well written technically or not, between the REMarks and the ECHOs of what's happening and when that code should be comprehensible to anyone that picks it up.

The same is true for some custom VBA scripting I did for a client to automate clicking a link when specific messages are received in Outlook. I've had to revisit that code as the text of the messages being watched for has been changed over time. Had I not commented exactly what I was doing, and how, I could never have updated it with ease. I learned what I know about VBA in order to complete that job and I've forgotten almost all of it. But I can still pick up that code and, from the comments, know what my intent was and what the code chunks that follow the comment chunks do.

I always keep that in mind when writing anything, even years after I was doing so on a day-to-day basis.
 
Have you tried to put the "pause" on a separate line, i.e.:
sfc xxxx
pause

When running batch scripts I found out, they actually pause if I click on their windows, and "un-pause" when I click again...
 
Back
Top