Delete Local User Profiles Remotely using a PowerShell Script (DelProf2.exe)

In this post we are going to look at utilizing a tool called DelProf and a small PowerShell script to delete user profiles on workstations remotely.

First we need to download the tool, it’s just one executable file that can be found here


The reason for using DelProf2.exe over the syntax’s Remove-Item or leveraging WMI to delete user profile folders, is you are guaranteed to run into permission problems with recursive junction folders when you aren’t using DefProf.

This blog post explains why junction folders are such a pain when it comes to automating local profile deletion. In short they are a symbolic link to another directory on the computer with Deny All access to Everyone.

If you have tried it yourself, you will be familiar with PowerShell errors similar to the one below:

Access to the path ‘\COMPUTERNAME\C$\Users\PROFILEFOLDER\AppData\Local\Application Data’ is denied. + CategoryInfo : PermissionDenied: (\COMPUTERNAME\C$…ncken-a\AppData:String) [Remove-Item ], UnauthorizedAccessException + FullyQualifiedErrorId : RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveIte mCommand + PSComputerName : COMPUTERNAME


After downloading DefProf2.exe-

Place the DelProf2.exe in a folder on your C: drive, call the folder delprof. This is important because the script will look for the executable at the filepath “C:\delprof\DelProf2.exe.”

delprof

After that create your small script-

When the script below is run , all you need to do is input the hostname of the workstation(s) and the name of the profile folder, this being the username of the account. Feel free to use wildcards if you want to target multiple workstations or profiles.

###############################################
#         Delete local profile folders        #
#      Cameron Yates - sysadminguides.org     #
###############################################

Param
(
[parameter(mandatory=$true,HelpMessage="Please type the Hostname of the workstation(s) Use wildcards for a collection")][ValidateNotNullOrEmpty()][String]$Hostname,
[parameter(mandatory=$true,HelpMessage="Please enter the user account (profile folder name)")][ValidateNotNullOrEmpty()][String]$UserProfile
)

If ($UserProfile -ne $null){

$Computers = Get-ADComputer -Filter {Name -like $Hostname}
ForEach ($computer in $computers) {
$ComputerN = $computer.Name
$runpath = "C:\delprof\DelProf2.exe"
$arguments = "/u /c:\\$ComputerN /id:$UserProfile"
Start-Process -filepath $runpath -Verb runAs -ArgumentList $arguments 
}}

 

User Friendly Version – Asks for confirmation to run and has more instructions

###############################################
#         Delete local profile folders        #
#          Created by - Cameron Yates         #
###############################################

Param
(
[parameter(mandatory=$true,HelpMessage="Please type the Hostname of the workstation(s) Use wildcards for a collection")][ValidateNotNullOrEmpty()][String]$Hostname,
[parameter(mandatory=$true,HelpMessage="Please enter the user account (profile folder name)")][ValidateNotNullOrEmpty()][String]$UserProfile
)

If ($UserProfile -ne $null){
    Write-host ' '
    Write-host '**Make sure you have created a folder on your C: drive called "del prof" containing DelProf2.exe** ' -foreground "Yellow"
    Write-host ' '
    Write-host 'Are you happy with your inputs? Wildcards (*) in this instance can be dangerous'
    $Command = Read-Host "Enter Yes continue"
        If($Command -eq "Yes"){
            $Computers = Get-ADComputer -Filter {Name -like $Hostname}
                ForEach ($computer in $computers) {
                $ComputerN = $computer.Name
                $runpath = "C:\delprof\DelProf2.exe"
                $arguments = "/u /c:\\$ComputerN /id:$UserProfile"
                Start-Process -filepath $runpath -Verb runAs -ArgumentList $arguments 
                }}
        Else{
            Write-host ' '
            Write-host 'Incorrect input, please re-run the script and next time input Yes to confirm you are happy for the script to run' -foreground "Red"
            Write-host ' '
            $host.enternestedprompt()
            }
}  

 

Delete profile script

What the script does is it uses your input for the  variable $Hostname to query AD for the workstation(s), to which it then runs DelProf2.exe against that workstation(s) from the executable on your machine. Delprof will then query the remote workstations C:\Users filepath, and delete the profile folder(s) you specified in the $UserProfile variable.

That’s it – you are all set!

Hope this was helpful – enjoy effortlessly deleting local profiles from workstations remotely.

 

 


Thanks for reading – feel free to follow and stay updated 🙂  View sysadminguides’s profile on Facebook View GuidesSysadmin’s profile on Twitter View 115372466162675927272’s profile on Google+

7 thoughts on “Delete Local User Profiles Remotely using a PowerShell Script (DelProf2.exe)

  1. Hello blogger, i must say you have hi quality posts here. Your website should go viral.
    You need initial traffic boost only. How to get it?

    Search for: Mertiso’s tips go viral

    Like

  2. Hello,

    I tried this and it doenst work for me. The black window show’s for an instant with the correct pc and user. But the user profile still excists on the remote pc.
    The script is excaxt the same as above.

    Like

  3. Hi again,
    I found something if i run the script and give as hostname a server (win2012R2) then it deletes profiles. But if i run the script and gives as hostname W10 client, it doesnt.
    So i logged in on a w10 client, i ran delprof2 –> a black screen appears ask me to i want to delete all profiles, YES –> nothing happens.
    If i run delprof2 as administrator –>a black screen appears ask me to i want to delete all profiles, YES –> Delprof2 deletes all profiles.
    I think dellprof doesnt run as administrator on the remote W10 Client so it cant delete profiles.

    Sorry for my english, its not the best.

    Like

    • Sorry for the late reply, makes me wonder whether it works with Windows 10. Pretty sure it does but most of the clients I work on are still on Windows 7. I’ll have to do some testing. Thanks for the info

      Like

    • Hello Recep, makes me wonder is the tool is windows 10 compatible. I’ve used it mainly for windows 7 machines and my current job hasn’t required the use of this script so I haven’t had the chance to test it.

      Like

  4. According to the Delprof2 website it has issues with Windows 10. And it looks like the developer has no plans to work around it.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s