Hello Gareth,
It is not necessary to use scripts for this purpose. You can use a built-in action, Active or modify Microsoft 365 account. In the action, use option Modify properties. In the settings, you can add only the licenses that should be revoked. In the following example, only the Microsoft E5 license will be revoked and other license will remain untouched.