From the Super Mario Wiki
The Vanish Cap is a cap found in Super Mario 64. When worn, it turns Mario temporarily into Vanish Mario. In this form Mario can walk through metal fences and certain walls. Mario must first press the blue ! Switch in order to activate the blue ! Blocks which contain vanish caps.
The Vanish Cap is found only in Super Mario 64. In the Nintendo DS remake, Super Mario 64 DS, Luigi can use the power granted by the vanish cap by using a Power Flower, but the Vanish Cap itself is absent.