Hello,

I am looking for a PS script that I can run that will nuke specific user profiles off of a particular machine remotely. I have the profdel tool from MS but I was really hoping to run a script so I would not have to use that or touch each machine. Anyone have any luck with this? Any and all help is greatly appreciated.

4 Spice ups

It’s just like removing a regular folder.

What have you tried? Where are you stuck?

invoke-command  -computername $computernameHere -scriptblock{
    remove-item "c:\Users\$FolderNameHere" -Recurse -Force
}

summat like so

If you post code, please use the ‘Insert Code’ button. Please and thank you!

Does this remove the hive entries in the registry as well, or does this only delete the profile folder? I am very new to PS scripts and I have no clue where to start for something like this. I want to make sure the entire profile gets deleted and not just the profile folder. Thank you for the reply.

@alexw

PowerShell is like any other scripting / programming language. It only does what you tell it to do. This only removes the user directory.
If you need to remove a registry entry you have to add more code to do that.

Remove-Item -Path "HKCU:\some\reg\key" -Recurse
1 Spice up

Look at this: https://community.spiceworks.com/how_to/124316-delete-user-profiles-with-powershell

Solution might already exist.

2 Spice ups

I initially replied to your duplicate thread but I’ll move my reply here. You should probably remove the other thread.

I don’t have a full script for you, but here is a general idea:

$computerName = 'Your-Remote-PC-Name'
$SID = 'SID-of-user-you-want-to-remove'
$Credential = Get-Credential # specify credentials for connecting to the remote computer
Invoke-Command -ComputerName $ComputerName -Credential $Credential -ArgumentList $SID -ScriptBlock {
    Param($SID)
    Get-WMIObject -class Win32_UserProfile | ?{$_.SID -eq $SID} | Remove-WmiObject
}

If I remember correctly this would remove the entire profile, both files and registry. You might want to add some checks to make sure that user isn’t currently logged in or that the SID actually exists before you try to delete it. You might also not need the $credential part, if you’re already running this script as a user with permissions to connect to your target PC.

I’m assuming that you have PS remoting enabled on your target machine, obviously. Also, I think you could also do this without the Invoke-Command and just using the WMI remoting natively if you wanted. Wrapping it this way though means you can add in some of those other checks I mentioned, maybe back up the user’s folder first, etc.

That’s a little fragmented but I hope it’s helpful. Good luck!

3 Spice ups

Thanks. My browser glitched when I was submitting the question. I did not know it posted twice. I deleted the other one.

@dannease

This is the correct approach.

You could also call the Win32_UserProfile.Delete() method once you have the correct profile object. This is generally faster than Remove-WmiObject (because of the commandlet binding overhead), but that is only really noticeable when you need to do bulk deletes.

3 Spice ups

I had to adjust this ps script to suit my environment, but it does work.

https://gallery.technet.microsoft.com/scriptcenter/Delete-user-profiles-over-05348eef

I run it from a server and it references a text file of computers I want it to look at. Then it presents a box where I can select the user profile I want to delete and goes back through the computers to delete that one profile.

1 Spice up

I will give this one a try. Thank you!

@aileencjr

1 Spice up