- Automatically start a Windows Service on install
- 13 Answers 13
- Autoruns for Windows v13.98
- Introduction
- Screenshot
- Usage
- Autorunsc Usage
- Related Links
- Download
- Windows 8 auto start a program before user login
- 1 Answer 1
- “Automatic” vs “Automatic (Delayed start)”
- 1 Answer 1
- How to run a program automatically as admin on Windows 7 at startup?
- 9 Answers 9
Automatically start a Windows Service on install
I have a Windows Service which I install using the InstallUtil.exe. Even though I have set the Startup Method to Automatic, the service does not start when installed, I have to manually open the services and click start. Is there a way to start it either via the command line, or through the code of the Service?
13 Answers 13
In your Installer class, add a handler for the AfterInstall event. You can then call the ServiceController in the event handler to start the service.
Now when you run InstallUtil on your installer, it will install and then start up the service automatically.
After refactoring a little bit, this is an example of a complete windows service installer with automatic start:
How about following commands?
Programmatic options for controlling services:
- Native code can used, «Starting a Service». Maximum control with minimum dependencies but the most work.
- WMI: Win32_Service has a StartService method. This is good for cases where you need to be able to perform other processing (e.g. to select which service).
- PowerShell: execute Start-Service via RunspaceInvoke or by creating your own Runspace and using its CreatePipeline method to execute. This is good for cases where you need to be able to perform other processing (e.g. to select which service) with a much easier coding model than WMI, but depends on PSH being installed.
- A .NET application can use ServiceController
You can use the following command line to start the service:
Use ServiceController to start your service from code.
Update: And more correct way to start service from the command line is to use «sc» (Service Controller) command instead of «net».
Autoruns for Windows v13.98
By Mark Russinovich
Published: June 24, 2020
Download Autoruns and Autorunsc (2.5 MB)
Run now from Sysinternals Live.
Introduction
This utility, which has the most comprehensive knowledge of auto-starting locations of any startup monitor, shows you what programs are configured to run during system bootup or login, and when you start various built-in Windows applications like Internet Explorer, Explorer and media players. These programs and drivers include ones in your startup folder, Run, RunOnce, and other Registry keys. AutorunsВ reports Explorer shell extensions, toolbars, browser helper objects, Winlogon notifications, auto-start services, and much more.В Autoruns goes way beyond other autostart utilities.
Autoruns‘В Hide Signed Microsoft EntriesВ option helps you to zoom in on third-party auto-starting images that have been added to your system and it has support for looking at the auto-starting images configured for other accounts configured on a system. Also included in the download package is a command-line equivalent that can output in CSV format, Autorunsc.
You’ll probably be surprised at how many executables are launched automatically!
Screenshot
Usage
Simply runВ AutorunsВ and it shows you the currently configured auto-start applications as well as the full list of Registry and file system locations available for auto-start configuration. Autostart locations displayed by Autoruns include logon entries, Explorer add-ons, Internet Explorer add-ons including Browser Helper Objects (BHOs), Appinit DLLs, image hijacks, boot execute images, Winlogon notification DLLs, Windows Services and Winsock Layered Service Providers, media codecs, and more. Switch tabs to view autostarts from different categories.
To view the properties of an executable configured to run automatically, select it and use theВ PropertiesВ menu item or toolbar button. If Process ExplorerВ is running and there is an active process executing the selected executable then theВ Process ExplorerВ menu item in theВ EntryВ menu will open the process properties dialog box for the process executing the selected image.
Navigate to the Registry or file system location displayed or the configuration of an auto-start item by selecting the item and using theВ JumpВ to Entry menu item or toolbar button, and navigate to the location of an autostart image.
To disable an auto-start entry uncheck its check box. To delete an auto-start configuration entry use theВ DeleteВ menu item or toolbar button.
The Options menu includes several display filtering options, such as only showing non-Windows entries, as well as access to a scan options dialog from where you can enable signature verification and Virus Total hash and file submission.
Select entries in theВ UserВ menu to view auto-starting images for different user accounts.
More information on display options and additional information is available in the on-line help.
Autorunsc Usage
Autorunsc is the command-line version of Autoruns. Its usage syntax is:
Usage: autorunsc [-a ] [-c|-ct] [-h] [-m] [-s] [-u] [-vt] [[-z ] | [user]]]
Parameter | Description |
---|---|
-a | Autostart entry selection: |
* | All. |
b | Boot execute. |
d | Appinit DLLs. |
e | Explorer addons. |
g | Sidebar gadgets (Vista and higher) |
h | Image hijacks. |
i | Internet Explorer addons. |
k | Known DLLs. |
l | Logon startups (this is the default). |
m | WMI entries. |
n | Winsock protocol and network providers. |
o | Codecs. |
p | Printer monitor DLLs. |
r | LSA security providers. |
s | Autostart services and non-disabled drivers. |
t | Scheduled tasks. |
w | Winlogon entries. |
-c | Print output as CSV. |
-ct | Print output as tab-delimited values. |
-h | Show file hashes. |
-m | Hide Microsoft entries (signed entries if used with -v). |
-s | Verify digital signatures. |
-t | Show timestamps in normalized UTC (YYYYMMDD-hhmmss). |
-u | If VirusTotal check is enabled, show files that are unknown by VirusTotal or have non-zero detection, otherwise show only unsigned files. |
-x | Print output as XML. |
-v[rs] | Query VirusTotal for malware based on file hash. Add ‘r’ to open reports for files with non-zero detection. Files reported as not previously scanned will be uploaded to VirusTotal if the ‘s’ option is specified. Note scan results may not be available for five or more minutes. |
-vt | Before using VirusTotal features, you must accept the VirusTotal terms of service. If you haven’t accepted the terms and you omit this option, you will be interactively prompted. |
-z | Specifies the offline Windows system to scan. |
user | Specifies the name of the user account for which autorun items will be shown. Specify ‘*’ to scan all user profiles. |
Related Links
- Windows Internals Book The official updates and errata page for the definitive book on Windows internals, by Mark Russinovich and David Solomon.
- Windows Sysinternals Administrator’s Reference The official guide to the Sysinternals utilities by Mark Russinovich and Aaron Margosis, including descriptions of all the tools, their features, how to use them for troubleshooting, and example real-world cases of their use.
Download
Download Autoruns and Autorunsc (2.5 MB)
Run now from Sysinternals Live.
Windows 8 auto start a program before user login
Am able to auto-start my program by copying its shortcut into the start-up folder. However i want the program to auto-start on completing boot, even before any user is logged in. How is this possible?
1 Answer 1
I found this for windows 7 & maybe it’ll work with Windows 8 too.
If you want it to start before the user logs on, you will have to start it as a service.
Here is the startup sequence of the major registry keys, starting immediately after bootmgr has been read and ending with the program shortcut entries in the two Startup folders.
- HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\BootExecute. This can include instructions to schedule the running of chkdsk but not user programs.
- Services start next, followed by the RunServicesOnce and RunServices registry keys (if present)
- User then logs on to the system
- HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\UserInit. This points to the program C:\WINDOWS\system32\userinit.exe and the entry ends with a comma. Other programs can be started from this key by appending them and separating them with a comma.
- HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell. This should contain just one entry, explorer.exe.
- Program entries in these 2 registry keys for ALL USERS start next: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run and \RunOnce
- Program entries in these 2 registry keys for CURRENT USER start next: HKCU\Software\Microsoft\Windows\CurrentVersion\Run and \RunOnce
- Programs in the Startup Folders of All Users and Current User are started last of all.
Important programs like antivirus and firewall start early in the sequence as Services. The icons that appear in the Notification Area (bottom right of the screen) are just their user interfaces, i.e. options and preferences.
The additional location for 32-bit software in a 64-bit computer is HKLM\SOFTWARE\Wow6432Node and HKCU.
“Automatic” vs “Automatic (Delayed start)”
When installing Windows services there are two options for automatically starting a Windows service on Windows startup. One is Automatic, and the other is Automatic (Delayed start). What is the difference between these two in detail?
For example, if you’re creating the installer with wixtoolset, the ServiceConfig element has the DelayedAutoStart attribute. How will that effect what happens when services are started at boot time?
1 Answer 1
In short, services set to Automatic will start during the boot process, while services set to start as Delayed will start shortly after boot.
Starting your service Delayed improves the boot performance of your server and has security benefits which are outlined in the article Adriano linked to in the comments.
Update: «shortly after boot» is actually 2 minutes after the last «automatic» service has started, by default. This can be configured by a registry key, according to Windows Internals and other sources (3,4).
The registry keys of interest (At least in some versions of windows) are:
- HKLM\SYSTEM\CurrentControlSet\services\ \DelayedAutostart will have the value 1 if delayed, 0 if not.
- HKLM\SYSTEM\CurrentControlSet\services\AutoStartDelay or HKLM\SYSTEM\CurrentControlSet\Control\AutoStartDelay (on Windows 10): decimal number of seconds to wait, may need to create this one. Applies globally to all Delayed services.
How to run a program automatically as admin on Windows 7 at startup?
I created my own parental control app to monitor my kids activity. The app’s only GUI is a task bar icon. The program is installed as admin. I’d like this program to be started up automatically as admin user on Windows startup, so that standard users cannot kill it from task manager.
I can create a registry key at:
to make it run automatically when Windows starts up. The problem is that the program is started as the logged in (standard) user.
How can I make it run in an elevated mode? Is this possible at all in Win7?
9 Answers 9
You need to plug it into the task scheduler, such that it is launched after login of a user, using a user account that has administrative access on the system, with the highest privileges that are afforded to processes launched by that account.
This is the implementation that is used to autostart processes with administrative privileges when logging in as an ordinary user.
I’ve used it to launch the ‘OpenVPN GUI’ helper process which needs elevated privileges to work correctly, and thus would not launch properly from the registry key.
From the command line, you can create the task from an XML description of what you want to accomplish; so for example we have this, exported from my system, which would start notepad with the highest privileges when i log in:
and it’s registered by an administrator command prompt using:
this answer should really be moved over to one of the other stackexchange sites, as it’s not actually a programming question per se.