This topic describes how to prepare user-data scripts for Windows instances and how to pass in user data and verify the result of running the user data.
Prerequisites
If you want to modify the user data of an instance, make sure that the instance is in the Stopped state.Warning Service interruptions may occur when you stop instances. We recommend that you stop instances during off-peak hours.
Background information
The user data feature enables Windows instances to run initialization scripts. When an instance starts, the system uses the administrator permissions to run the user data of the instance.The following limits apply to user data:
- The user data feature is supported only for instances that reside in virtual private clouds (VPCs).
- The instances must be created from the following public images or custom images derived from public images:
- Alibaba Cloud Linux, CentOS, CentOS Stream, Ubuntu, SUSE Linux Enterprise Server, Red Hat Enterprise Linux, OpenSUSE, Debian, AlmaLinux, Rocky Linux, and Fedora
- Windows Server 2008 R2 and later
- The user data feature is supported for all available instance types. For retired instance types, the user data feature is supported only for I/O-optimized instances. For more information, see Retired instance types.
- The user data that you want to run must be encoded in Base64. The size of the user data cannot exceed 16 KB before it is encoded. Note You can enter the user data that has not been encoded in Base64 in the console. The console automatically encodes the user data in Base64. If you do not want to enter the user data in the console, you must encode it in Base64 on your own.
Procedure
- Prepare user data. You can run batch and PowerShell scripts to prepare user data of Windows instances. For more information about the characteristics of different scripts and their examples, see the following sections:
- Pass the user data into an instance.
- Pass in the user data when you create an instance. In the System Configurations (Optional) step, click Advanced to show advanced options and enter the user data in the User Data field. If the user data is encoded in Base64, select Enter Base64 Encoded Information. The following figure shows sample user data that is used to write content to a specific file.
- Modify the user data of an existing instance: On the Instances page, find the instance for which you want to modify the user data and choose . In the Set User Data dialog box, enter new user data in the User Data field. Note If you want to start a pay-as-you-go instance immediately after you modify the user data of the instance, we recommend that you set the stop mode of the instance to Keep Instances and Continue Billing.The following figure shows sample user data that is used to write content to a specific file.Note After you modify the user data for a Windows instance, the new user data is not run when you start the instance.
- Pass in the user data when you create an instance. In the System Configurations (Optional) step, click Advanced to show advanced options and enter the user data in the User Data field. If the user data is encoded in Base64, select Enter Base64 Encoded Information.
- View the content passed into the instance and the script results.
Bat
Batch scripts have the following characteristics:
- The first line starts with
[bat]
, and the header cannot have spaces. - Only half-width letters can be entered, and no additional characters are allowed.
Example:
[bat]
echo "bat test" > C:\userdata_test.txt
The example batch script can be run to write 
"bat test"
to the userdata_test.txt file when the instance starts for the first time, as shown in the following figure. 
PowerShell
PowerShell scripts have the following characteristics:
- The first line starts with
[powershell]
, and the header cannot have spaces. - Only half-width letters can be entered, and no additional characters are allowed.
Example:
[powershell]
write-output "powershell test" | Out-File C:\userdata_test.txt
The example PowerShell script can be run to write 
powershell test
to the userdata_test.txt file when the instance starts for the first time, as shown in the following figure. 