An electrically switchable diffractive optical element has been built based on a computer-generated phase hologram and a liquid-crystal layer. The design of the diffractive optical element and the fabrication and performance of the device are discussed. The application of the device in a machine vision system for optical surface inspection is shown.