How do I run a command on an existing EC2 Windows instance when I reboot or start the instance?

3 minute read

I want to run a custom script on my existing Amazon Elastic Compute Cloud (Amazon EC2) Windows instance when I reboot or start the instance.

Short description

Add a script to user data, and then configure it to run after an instance reboots or when an instance starts. EC2Config (Windows Server 2012 R2 and earlier), EC2Launch (Windows Server 2016 and 2019) or EC2LaunchV2 (Windows Server 2022) process user data.


When you add the script to user data, you must enclose it within a special tag. This tag determines whether the commands run in a Command Prompt window or in Windows PowerShell. For more information, see Run commands on your Windows instance at launch.

You can't add or change user data when the instance is running, but you can view it. To add or change user data, the instance must be in the stopped state. User data scripts don't automatically run when you reboot or start the instance. You can configure the user data to run the next time, or every time, that you reboot or start the instance.


  • If you don't use an Elastic IP Address, then the public IP address is released when you stop the instance.
  • If the instance has an instance store volume, then any data on it is lost when the instance is stopped.
  • If the instance shutdown behavior is set to Terminate, then the instance terminates when it's stopped.
  • If the instance is part of an Auto Scaling group, then first detach the instance from the Auto Scaling group. Then, after you stop and start the instance, attach the instance again to the Auto Scaling group. For more information, see Stop and start your instance.

1.    Connect to your EC2 Windows instance.

2.    For Windows Server 2012 R2 or earlier, open C:\Program Files\Amazon\Ec2ConfigService\Ec2ConfigServiceSetting.exe. Then, for User Data, select Enable UserData execution for next service start.
Note: The preceding command changes the local administrator password of the instance at every restart. If you use an AMI with the No Key Pair option selected, then you might lose access to your instance on restart.

For Windows Server 2016 and 2019, open a Windows PowerShell command window, and then run the following command:

C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule

For Windows Server 2022, run UserData as a script and set the frequency to always run on every boot. For more information, see executeScripts for syntax and examples.

3.    From the Amazon EC2 console, stop the instance.

4.    Select the instance and choose Actions. Then, choose Instance Settings, and then choose Edit User Data.

5.    Add or update the user data, and then choose Save.
Note: To run user data scripts every time you reboot or restart the instance, add the following setting:


6.    Start the instance. The user data scripts run as part of the instance start process.
For additional troubleshooting, EC2Config, EC2Launch, and EC2LaunchV2 log files contain the output from the standard output and standard error streams. Log files are located as follows:

  • EC2Config: C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2Config.log
  • EC2Launch: C:\ProgramData\Amazon\EC2-Windows\Launch\Log\UserdataExecution.log
  • EC2LaunchV2: C:\ProgramData\Amazon\EC2Launch\log\agent.log

Related information

How do I run a command on a new EC2 Windows instance at launch?

AWS OFFICIALUpdated a year ago