Try running Optimize-VDisk instead. If that returns a "command not found" error, ensure you have the Hyper-V module installed (see Solution 3).
Or, specifically for the disk image optimization action: optimize-vhd not found
However, many users—especially those running Windows 10 or Windows 11 Pro—often run into a frustrating blocker: Try running Optimize-VDisk instead
Sometimes the module exists but isn’t auto-loaded. optimize-vhd not found