When you login to the Windows for the first time windows creates a user profile and interactively logs on at the computer and it contains user registry (ntuser.dat), user-specific folders (My Documents, Desktop, etc), and Application Data folder that stores software-specific data pertaining to the user.
Deleting the user profile from the physical PC is easy but now we will be looking at how to delete a user profile from the command line tool remotely.
Using the below command we are trying to display a list of users whose profile has not been used for more than 60 days. You can change the value of the LastUseTime profile field for searching for any number of days as per your needs.
To remove all these profiles, it is enough to add a redirect list to the Remove-WmiObject command (before using the removal script, it is advisable to double-check its output using the -WhatIf parameter):
In order not to delete the profiles of some users, for example, special System and Network Service accounts, a local administrator account, users with active sessions, a list of excluded accounts), you need to modify the script as follows:
You can configure the launch of this script through the shutdown script of the Group Policy or on a schedule by the scheduler task. (Before setting up automatic deletion of profiles, test the script carefully in your environment!).
Now furthermore you can also modify this script to automatically delete users of all users who are added to a specific AD group. For example, if the user leaves from the company you can maintain the profile for a number of days and later you can delete it automatically without remembering.