I just googled it, here is one way I found:
X5, E46, E38 & E39 Diamond Shaped Key
To carry out this procedure you need to have one working key and one key which requires programming. Please read on if you do not have a working key as this is also covered further down. It may help sometimes to hold the key close to the rear view mirror as this is where the sensor is located in most models.
1. With both keys, get in the car and close all windows and doors.
2. Put working key into the ignition and turn to position 1 (the accessories should turn on)
3. Turn the key back to position 0 and take the key out of the ignition.
4. Using the working key, hold down the unlock button, press the BMW logo (lock button) 3 times, and release the unlock button. Doors should lock and unlock.
5. With the new key, hold down the unlock button and press the BMW logo (lock button) 3 times and let go of the unlock button. Doors should lock and unlock and you're done!!
1. Switch ignition on and off within 5 seconds, with all doors closed.
2. Take key out of ignition and press unlock button. Hold unlock button whilst pressing the lock button 3 times, all within 10 seconds.
3. Release unlock button.
4. The system recognises re-programming by locking and immediately unlocking the doors.
5. You have to do your other keys within 30 seconds after reprogramming your first key by following steps 2,3 & 4 only.
Alternate procedure if above doesn't work or if you do not have a working remote:
1. Turn key to position 1 five times very quickly
2. Remove key
3. Hold unlock button then press lock button 3 times, release unlock button.
4. If you have another key do the same button pressing within 30 seconds
5. Turn on ignition to finalize
Older 3 Button Key (E36, E38, E39)
Remote Key Re-Initialise - If a key fails to operate remotely, it will have to be re-initialised (all keys in sequence at the same time)
1. Get in and close all doors
2. Turn the ignition on & off quickly (no more than 5 seconds) to start the process. Next action must take place within 30 seconds
3. First key - hold down button #2 (unlock - on the bottom left with the key blade pointing up) while striking button #1 (lock - the BMW logo) three times. Release button #2. This should be confirmed by door lock operation.
4. If you have more than 1 key then you need to repeat for the other keys within 30 seconds of first or they will be rendered inactive.
5. After last key then cycle the ignition on/off to finalise the process (it is not necessary to start the engine).
Both keys should now have new randomly selected security code words - assigned by the car's system.
Those codes will be continuously incremented within the new sequence each time you hit a button. It is when they get out of sequence with the car that they loose the capability to be recognised and you then have to do this again.
This occurs when you take longer than 1 minute changing the battery, or if you hit the buttons more than 1000 times when it is out of range of the car.
The above was from link w/ directions to program key
and also at Reprogramming Key Fob on BMW X5 - Xoutpost.com
Also at dead key fob needs reprogramming [Archive] - Bimmerforums - The Ultimate BMW Forum
Again, another way is for just the fob: The fob has a chip inside the remote. You take out the chip. Under the back seat of the car, drivers side is the remote key unit. Open up this unit, there is a chip socket there. Plug the chip in the socket. Turn the ignition and the chip will get programmed. Then take out the chip and replace it into the key fob. Why is that so hard to believe? It seems logical to me. There seem to be many different ways, depending on the model. This is just for the chip. The actual key that goes into the ignition can be made at a pro locksmith shop.