I tested with simple PowerShell script mkdir C:\tempdir
When I specify it as UserData for instance launched from Amazon image, everything works fine. If I launch instance with specified UserData and make image of that instance, UserData is not executed for instances created from my custom image.
What is interesting, "C:\Program Files\Amazon\Ec2ConfigService\Scripts\UserScript.ps1"
contains UserData of initial launch (before image was created), although, it should contain data specified during last launch.
More interesting, even UserScript.ps1 is present, it is not executed (it should create text file but text file is not present).
Here is UserData from second launch
<powershell>
mkdir C:\tempdir
</powershell>
User data scripts only run once when the instance is launched (created not started up). They are then disabled on the instance. So when when you grab an AMI from that instance and launch a new one, user data has already been disabled.
Enable UserData
To allow it run again when you launch a new one from an AMI you created you must turn user data support back on at the EC2ConfigService Settings utility by checking the UserData box (before grabbing the AMI).
Persist Enabling UserData
Now after this if you reboot the instance again then the UserData will run and again disable itself. To disable this functionality too you will need to add <persist>true</persist>
to the instance's UserData which will keep the checkbox in the same state (on if you turn it on).
More info: