How can I remove domain profiles in Windows while excluding the logged-in user?
Asked by
TechWanderer42
Hey everyone! I'm trying to figure out how to remove domain profiles from Windows machines without affecting the currently logged-in user. I noticed that when I run my script, it indicates that the user is 'SYSTEM'. I would appreciate it if someone could take a look at my script and help me identify what I might be doing wrong. I'm deploying this script through RMM tools. Thanks in advance for any advice! Here's the script: https://pastebin.com/BAVQg3gH
Answers
If your script runs as System, that's why it always shows SYSTEM as the user! To get the currently logged-in user, run the script as that user instead. Also, have you considered using Group Policy to delete profiles after a certain period of inactivity? It can simplify things for you. Additionally, consider the CIM method for deleting user profiles rather than just messing with the registry.
Reply to this answer
It sounds like you're having a tough time with profile removal. Whatโs your end goal here? Are you trying to clear out old profiles from the machines? Just want to make sure I understand your intentions before suggesting a better method!
Reply to this answer
Check out this guide for using GPO to delete user profiles older than a certain number of days: https://learn.microsoft.com/en-us/archive/technet-wiki/28647.group-policy-how-to-automatically-delete-user-profiles-older-than-certain-number-of-days
Reply to this answer
Running RMM tools typically means they operate as LocalSystem. You can get the current user with this command: (Get-CimInstance -ClassName Win32_ComputerSystem).UserName. Also, have you thought about doing it the standard way instead? Instead of deleting from the registry, try using: Get-CimInstance -ClassName Win32_UserProfile | Remove-CimInstance. It might save you some headache!
Reply to this answer
We actually use Group Policy for managing profiles on our end. It's a lot smoother that way!
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions