Running a DOS command with options when I reboot the system

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Running a DOS command with options when I reboot the system

#1 Post by balubeto » 16 Apr 2012 02:51

HI

In Windows 7, there is a registry key/entry that allows to run a DOS command with options when I reboot the system (this command will have to be performed only on the first reboot)?

THANKS

BYE

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Running a DOS command with options when I reboot the sys

#2 Post by foxidrive » 16 Apr 2012 04:39

google for "runonce"

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Running a DOS command with options when I reboot the sys

#3 Post by balubeto » 16 Apr 2012 10:40

There is a problem:

The runonce key is performed in the system starting phase and not during the system shutdown phase. Am I wrong?

THANKS

BYE

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Running a DOS command with options when I reboot the sys

#4 Post by Squashman » 16 Apr 2012 10:46

You seem to be contradicting yourself. Do you want it to run on startup or shutdown?

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Running a DOS command with options when I reboot the sys

#5 Post by balubeto » 16 Apr 2012 11:12

During the shutdown.

THANKS

BYE

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Running a DOS command with options when I reboot the sys

#6 Post by miskox » 16 Apr 2012 11:49

- start -> run -> gpedit.msc
- Under computer configuration -> windows settings -> scripts -> right click on shutdown -> select properties -> add the name of the program you want to run (.cmd, .bat, .vbs...). Just try it.
I heared once that there might be a time limit on this - you better check this.

Saso

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Running a DOS command with options when I reboot the sys

#7 Post by balubeto » 16 Apr 2012 12:07

Instead, if I have Windows 7 Home Premium, how do I solve this problem?

THANKS

BYE

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Running a DOS command with options when I reboot the sys

#8 Post by foxidrive » 16 Apr 2012 16:42

.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Running a DOS command with options when I reboot the sys

#9 Post by abc0502 » 18 Apr 2012 21:03

this is the registry keys for runonce jobs:

Code: Select all

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce


u can add jobs using batch like that:

Code: Select all

@echo off
cls
set regkey1=HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
set regkey2=HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
:: = = = = = = =
REG add "%regkey1%" /v "job_name" /t "job_type" /d "job_command" /f >nul

Replace the "job_name" with the title of the job u going to add.
Replace the "job_type" with the type of the registry key use "REG_SZ" it will work.
Replace the "job_command with the program u want to launch and u can add special switches to it like "c:\test\example.exe /s"

if running windows 7 u will need permission to modify Registry.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Running a DOS command with options when I reboot the sys

#10 Post by foxidrive » 19 Apr 2012 00:56

@abc0502 The fellow wasn't clear - he wants to run a task when he *shuts down* the PC.


@op

One batch solution is to run the task in a batch file and then use shutdown.exe after it, so that running the batch file performs your tasks and then your machine shuts down.

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Running a DOS command with options when I reboot the sys

#11 Post by balubeto » 19 Apr 2012 01:43

abc0502 wrote:this is the registry keys for runonce jobs:

Code: Select all

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce


u can add jobs using batch like that:

Code: Select all

@echo off
cls
set regkey1=HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
set regkey2=HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
:: = = = = = = =
REG add "%regkey1%" /v "job_name" /t "job_type" /d "job_command" /f >nul

Replace the "job_name" with the title of the job u going to add.
Replace the "job_type" with the type of the registry key use "REG_SZ" it will work.
Replace the "job_command with the program u want to launch and u can add special switches to it like "c:\test\example.exe /s"

if running windows 7 u will need permission to modify Registry.


There is a problem:

The runonce key is performed in the system starting phase and not during the system shutdown phase. Am I wrong?

THANKS

BYE

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: Running a DOS command with options when I reboot the sys

#12 Post by phillid » 19 Apr 2012 02:15

Correct, runonce is for when the system is starting.

Maybe we could rename shutdown.exe to something else and put another exe in its place. This exe could store the argument that was intended for the shutdown command (-s, -r etc.), then runs a batch file we want to run on shutdown and then runs the original shutdown.exe (the one we renamed) with the argument we collected earlier....?

I know, I know, it's a bit iffy when messing with system files... Just a thought.

Hmm.. this is an afterthought... if an application uses a system call to shut the computer down, the solution I suggested may not work as Windows may not use shutdown.exe to shut the machine down when a system call is used to initiate a shutdown..... Can anyone confirm this?

Thanks
Phillid

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Running a DOS command with options when I reboot the sys

#13 Post by foxidrive » 19 Apr 2012 02:48

When Windows shuts itself down I'd think it uses an API function and not shutdown.exe

A test would be to rename shutdown.exe and see if Windows can be shut down.


The OP ignored my suggestion, so maybe it wasn't clear enough.
You can have a batch file like this to run a program before the machine shuts down.

@echo off
start "" /w "c:\program files\my application.exe"
shutdown.exe -s

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: Running a DOS command with options when I reboot the sys

#14 Post by phillid » 19 Apr 2012 02:54

Yeah that's what I thought.. :/ Time to decompile Windows, edit it and recompile ;)

The only problem with that batch file is that it won't run as a result of a shutdown... I think that's what he wants. Your batch shuts down the computer itself... Maybe he needs to clarify.

Anyway, time for me to have a coffee, enjoy the rest of the evening and get some sleep.

Thanks
Phillid

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Running a DOS command with options when I reboot the sys

#15 Post by abc0502 » 19 Apr 2012 03:18

sorry i didn't notice shutdown,
as miskox said use the group policy,
This should add registry keys to the group policy to run when shuting down the pc:
i tested it to add the registry keys but didn't test to start that keys:

Code: Select all

@echo off
cls

:: This is the main key and must be exist once to add jobs,
:: "regkey_main=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0"

:: This key is for job #1:
:: "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\0"
::This key is for job #3:
:: "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\1"
:: If Add 3rd Job the key should look like: "notice the last digits"
:: "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\2"

:: ===== Change This section Only with your settings =====
set "regkey=%temp%\reg.reg"

set "scriptname1=Test1"
set "parameters1=D:\example1.exe /s"

set "scriptname2=Test2"
set "parameters2=D:\example2.exe /s"

:: you can add more jobs by duplicating the last set of commands and consider the last number in each regkey address "0\0" for job 1 and "0\1" for job 2  etc,...
:: the %regkey_main appear once and don't repaeat it
:: =============== End Section ================
:: This is The Reg.Reg Key that will add the tasks executed at the shutdown ::
IF EXIST "%regkey%" ( DEL /F /Q "%regkey%
) Else ( goto next )
:next
echo: Windows Registry Editor Version 5.00 >>%regkey%
echo:
echo: ^[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0^] >>%regkey%
echo: "GPO-ID"="LocalGPO" >>%regkey%
echo: "SOM-ID"="Local" >>%regkey%
echo: "FileSysPath"="C:\\Windows\\System32\\GroupPolicy\\Machine" >>%regkey%
echo: "DisplayName"="Local Group Policy" >>%regkey%
echo: "GPOName"="Local Group Policy" >>%regkey%
echo: "PSScriptOrder"=dword:00000001 >>%regkey%
echo:
echo: ^[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\0^] >>%regkey%
echo: "Script"="scriptname1" >>%regkey%
echo: "Parameters"="parameters1" >>%regkey%
echo: "IsPowershell"=dword:00000000 >>%regkey%
echo: "ExecTime"=hex^(b^):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 >>%regkey%
echo:
:: The next commands is for a second job u can delete it if u have only one job and add one like it at the end if u have more jobs just notice the last digit of the reg-key
:: >"0\1"<   this mean 2nd job and if add 3rd job it will be like that   >"0\2"<   at the end of the same key name and the only change will be in   >"script"<   and   >"parameters"<.
echo: ^[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0\1^] >>%regkey%
echo: "Script"="scriptname2" >>%regkey%
echo: "Parameters"="parameters2" >>%regkey%
echo: "IsPowershell"=dword:00000000 >>%regkey%
echo: "ExecTime"=hex^(b^):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 >>%regkey%

Reg Import "%regkey%" >nul


just change the section pointed to in the batch and run it with administrator rights i tested in win7

Post Reply