Sorry to revive an old thread but I wanted to add some information.
Cyrix2k has already shown you how to DIY key programming using CGDI which is a quality tool for a reasonable price. However, there are chinese clones of other well-known tools that run far less in cost (though likely come with a bit more 'risk).
Programming a key to your BMW's CAS will cost you money, regardless if you DIY or pay someone to do it. The tools for you to DIY will be in the $200 range for chinese clones and the process takes 15 min.
1: It's FAR easier to program a virgin key than a 'used key'. Virgin comfort access and non comfort access keys are $15 USD on Aliexpress.
2: A used key will need to either have the transponder de-soldered and a new one soldered in ($4 per transponder chip) OR you can purchase a PCF79xx Unlocker, or Xhorse VVDI PROG Programmer, and fiddle around with that to erase the data from the transponder. As you can see, you're better off buying a virgin key!!!
3: You can use HiTag2 to prepare a blank key and create the file for use in BMW Explorer/EF Scanner.
4: With a battery tender/charger on your car, use EF Scanner to program the new key to the CAS.
5: If you're on a budget you can get your key blade cut/copied at a local lock smith for ~$15 USD... otherwise you can buy a Defu 368a key cutting machine just like Cyrix2k referenced.
Here's how:
https://youtu.be/MVO_iMznwb0