General speaking, it is recommended that you delete the snapshots in Hyper-V Manager console and then let the system merge the snapshots automatically while the virtual machines is power off.
By the way, according to the description, it seems that you want to write script. As we mainly focus on the Hyper-V question about Windows Server system and here is not the best support resource for script writing, it is recommend you to get further support
in the corresponding community so that you can get the most qualified pool of respondents. Thanks for your understanding.
For your convenience, I have list the related link as followed.