How to delete Windows user profile with PowerShell?

Helo, guys!

I need to be able to delete all user's profiles on all workstations in case of emergency. I have read similiar topics, but don't think it's suitable for me, so I need some help.

All workstations are working under Win7Pro, so I want to use PowerShell for this duty and I think I can use New-PSSession cmdlet to establish sessions to all my workstations, after that I need to log off users and delete profiles.

I tried to delete a profile at my localhost with next command:

( Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -eq 'c:\users\user' } ).Delete()

but get an exception: Exception calling "Delete" with "0" argument(s)

I thought this method needs some args, but I couldn't find its definition or syntax neither with Get-Member, nor at MSDN. Can you point me - what does this error mean?

By the way - is there an easy way to log off users from system with PowerShell?


Thanks for all replies!

May 23rd, 2011 10:10am

First, per MSDN, there is no Delete() method on Win32_UserProfile. See: http://msdn.microsoft.com/en-us/library/ee886409%28VS.85%29.aspx.

The expression: ( Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -eq 'c:\users\user' } ) will give you the profile object for 'user'. Why not just use invoke-command and specify  script block that deletes the remote profile folder. Something like:

 

$pro = ( Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.LocalPath -eq 'c:\users\user' } )

invoke-command -scriptblock {Remove-item $pro.localpath} -computer $comp

 

Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2011 5:11pm

Hi,

As Thomas mentioned, the Win32_Profile class has no Delete method. Also, I don't recommend simply deleting the directory as this doesn't remove the data associated with the profile from the registry (see Deleting a Local User Profile - Not as easy as one Might Assume). I haven't used it, but Helge Klein's delprof2.exe program looks like a workable replacement for Microsoft's Delprof utility (which looks like in only works on XP/2003 and older).

HTH,

Bill

May 23rd, 2011 6:27pm

Thanks guys!

I'll try what you say. About Delete() method, it is probably the one belongs to powershell base classes, because it is accessible with autocompletion. I've read MSDN article about Win32_UserProfile class, but I thought method Delete() is inherited from parent class, also I've found a couple of articles which advice use that method, they are probably wrong. Anyway, i'll try some other ways, thanks.

 

 

Free Windows Admin Tool Kit Click here and download it now
May 24th, 2011 9:27am

The Delete() method in WIn32_UserProfile works fine. And you are true that there is no information available on MSDN about this hidden method. But surely this is not something Powershell is creating. All PS is doing is using the functions that WMI has.

You may want to refer http://techibee.com/powershell/powershell-script-to-delete-windows-user-profiles-on-windows-7windows-2008-r2/1556 for a working example that deletes a user profile in a single or multiple comp

March 23rd, 2012 9:49pm

Win32_UserProfile  will not work on pre-Vista systems.

DElProf is a commandlineutility used for deleting profiles.

DELPROF [/Q] [/I] [/P] [/R] [/C:\\<computername>] [/D:<days>]

/Q      Quiet, no confirmation.
/I      Ignore errors and continue deleting.
/P      Prompts for confirmation before deleting each profile.
/R      Delete roaming profile cache only
/C      Remote computer name.

It has been available since NT4.

If you know that a profile is not is use and you know the user id then you can just delete the folder directly using the admin share or WMI Win32_Directory class.

Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2012 11:25pm

Thanks, jrv!

But all my workstations are running under Win7.

Dmitriy

March 24th, 2012 8:36pm

Thanks, Sitaram!

I've read your article a couple of months ago, but your script doesn't work either with the same error message as my own attempts (I don't remember that message already). Method delete() doesn't work with my Win 7 PRO SP1 x64 Russian, Active Directory member at all!

But it's a great example of PS scripting :)

Dmitriy



  • Edited by DmitriyPKCC Saturday, March 24, 2012 5:46 PM
Free Windows Admin Tool Kit Click here and download it now
March 24th, 2012 8:40pm

I think Moderators should close the discussion because there is no relatively easy way to remotly delete user profiles in Widows 7 SP1 without special software!

Thanks everybody for replies!

Dmitriy

March 24th, 2012 8:50pm

Hi,

That is correct; there is no method directly available from script (apart from an external executable that calls the appropriate Win32 API functions) that can delete a Windows user profile. That's why my answer (which you unmarked as an answer) is in fact an appropriate answer.

Bill

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2012 8:55pm

Thanks again, Bill!

But Win32_Profile class does have Delete method! And delprof2 doesn't work also, that's why I've unmarked your answer :) But I'll mark that again, thanks for trying to help!

Dmitriy

March 24th, 2012 9:30pm

I just ran a cleanup of unused profiles on WIndows 7 system in one of my domains.  I used DelProf.  It worked correctly with no issues.  It will delete roaming profiles and complain about local profiles (can't find file)

DelProf2 will not complain.

Win32_UserProfile.Delete() works well on Vista and later.  My suspicion is that it will become the de-facto tool for profile maintenance.

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2012 10:03pm

Dmitriy, Thanks for posting back. I don't see a reason why it shouldn't work if the target system is later than Windows Vista. I can help you further if you post the exact error message you are seeing while running the script.

One alternative you have is using a thirdparty tool called DelProf2.exe(not just delprof) which is designed to work with profiles on WIndows 7 Computers. I used this in a few automations and worked without any issues.

March 26th, 2012 8:45am

Hi,

An old thread but if you encounter "Exception calling "Delete" with "0" argument(s)" you might not be running the script as administrator.

I use this technique to wipe older profiles from my Win7 stations.

Get-WmiObject Win32_UserProfile | Where-Object {($_.Status -eq "2") -and (([WMI] '').ConvertToDateTime($_.LastUseTime) -lt ((Get-Date).AddDays(-15)))} | % {$_.Delete()}

It also works remotely on my servers ...

$Servers = @("rds1","rds2","rds3)


ForEach ($Server in $Servers)
{
    $Server
            Get-WmiObject Win32_UserProfile -ComputerName $Server | Where-Object {($_.Status -eq "2") -and (([WMI] '').ConvertToDateTime($_.LastUseTime) -lt ((Get-Date).AddDays(-15)))} | % {$_.Delete()}
}
    

Thanks,

John

  • Proposed as answer by JJONES10 Wednesday, July 31, 2013 3:11 PM
Free Windows Admin Tool Kit Click here and download it now
September 26th, 2012 12:27pm

I know its a old thread, but Johns script worked for me.
July 31st, 2013 6:12pm

I know its a old thread, but Johns script worked for me.

I second that...works great on 2012 RDS servers. Thanks.
Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2013 5:50pm

I apologize if I'm necro'ing a thread, but say I wanted to use John's script to delete a profile (or group of profiles) I already had in a list (.csv, string, array ... whatever).  How would I use WMI (and what would be the syntax involved) to delete a profile at that point?
January 30th, 2014 5:43pm

I apologize if I'm necro'ing a thread, but say I wanted to use John's script to delete a profile (or group of profiles) I already had in a list (.csv, string, array ... whatever).  How would I use WMI (and what would be the syntax involved) to delete a profile at t
Free Windows Admin Tool Kit Click here and download it now
January 30th, 2014 6:56pm

The Win32_userprofile does have a delete method and it works as required.  The DelProf2 is a wrapper around this method.

gwmi -list win32_userprofile

PS C:\scripts> $p=gwmi win32_userprofile
PS C:\scripts> $p[0].Delete

OverloadDefinitions
-------------------
void Delete()
void Delete(System.Management.DeleteOptions options)
void Delete(System.Management.ManagementOperationObserver watcher)
void Delete(System.Management.ManagementOperationObserver watcher, System.Management.DeleteOptions options)

January 26th, 2015 10:53pm

Hi,

As Thomas mentioned, the Win32_Profile class has no Delete method. Also, I don't recommend simply deleting the directory as this doesn't remove the data associated with the profile from the registry (see Deleting a Local User Profile - Not as easy as one Might Assume). I haven't used it, but Helge Klein's delprof2.exe program looks like a workable replacement for Microsoft's Delprof utility (which looks like in only works on XP/2003 and older).

HTH,

Bill

This is a easy to use, free cmd-line tool that respects the registry:

Delprof2 User Profile Deletion Tool

I don't understand...you quoted my post and then inserted a link to the exact same tool I referred to in my post.
Free Windows Admin Tool Kit Click here and download it now
January 26th, 2015 10:58pm

Sorry about that, i quoted the wrong reply.  I've deleted my post to eliminate confusion.
February 25th, 2015 2:44pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics