In fact, Michael's answer is about how to compact the vhd, while the original question was how to shrink the .vhd, which can (or not) be the same thing, depending on what the original user wanted. At the end of a compactation, the size of the virtual hard disk is the same (from the guest point of view), but it has been reorganized so that the .vhd file is smaller (from the host point of view).
However, if you need to shrink a dynamic VHD (from the guest point of view), so that at the end of the process your virtual machine has a smaller virtual hard drive (because you oversized them during the installation, or any other reason), please have a look at http://jagbarcelo.blogspot.com/2011/11/how-to-shrink-vhd-virtual-hard-disk.html
I hope it helps someone. Regards.