How to change ownership and give full permissions to a user on a folder (VBScript)

Some time ago we faced an issue where a customer renamed users in their domain, but their profile folders didn’t get renamed. So they wanted to automate the following process which worked for them when doing it manually: take ownership of a user’s profile folder with an administrator, give full permissions to that admin on the folder, rename it, and give full permissions back to that user.

The following VBScript sample shows how to give ownership to a user on a folder, how to give full permissions to a user on a folder, and how to rename the folder. For that it will use WMI to manipulate the Security Descriptor of the folder: