none
How to delete Windows user profile with PowerShell?

    Question

  • 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!

    Monday, May 23, 2011 7:10 AM

Answers

All replies

  • 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

     


    Thomas Lee Doctordns@Gmail.Com
    Monday, May 23, 2011 2:11 PM
    Moderator
  • 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

    Monday, May 23, 2011 3:27 PM
    Moderator
  • 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.

     

     

    Tuesday, May 24, 2011 6:27 AM
  • 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 computers.


    Thanks,

    Sitaram Pamarthi

    Blog : http://techibee.com

    Follow on Twitter

    This posting is provided AS IS with no warranties or gurentees,and confers no rights


    • Edited by pamarths Friday, March 23, 2012 6:50 PM
    Friday, March 23, 2012 6:49 PM
  • 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.


    ¯\_(ツ)_/¯

    Friday, March 23, 2012 8:25 PM
  • Thanks, jrv!

    But all my workstations are running under Win7.

    Dmitriy

    Saturday, March 24, 2012 5:36 PM
  • 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
    Saturday, March 24, 2012 5:40 PM
  • 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

    Saturday, March 24, 2012 5:50 PM
  • 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

    Saturday, March 24, 2012 5:55 PM
    Moderator
  • 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

    Saturday, March 24, 2012 6:30 PM
  • 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.


    ¯\_(ツ)_/¯


    • Edited by jrv Saturday, March 24, 2012 7:04 PM
    Saturday, March 24, 2012 7:03 PM
  • 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.


    Thanks,

    Sitaram Pamarthi

    Blog : http://techibee.com

    Follow on Twitter

    This posting is provided AS IS with no warranties or gurentees,and confers no rights

    Monday, March 26, 2012 5:45 AM
  • 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
    Wednesday, September 26, 2012 9:27 AM
  • I know its a old thread, but Johns script worked for me.
    Wednesday, July 31, 2013 3:12 PM
  • I know its a old thread, but Johns script worked for me.

    I second that...works great on 2012 RDS servers. Thanks.
    Thursday, October 03, 2013 2:50 PM
  • 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?

    zarberg@gmail.com

    Thursday, January 30, 2014 2:43 PM
  • 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?

    zarberg@gmail.com

    Please open new question and link to this.

    The simple answer is ti use the Delete method:

    $profile.Delete()


    ¯\_(ツ)_/¯

    Thursday, January 30, 2014 3:56 PM
  • 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)
    


    ¯\_(ツ)_/¯

    Monday, January 26, 2015 7:53 PM
  • 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.

    -- Bill Stewart [Bill_Stewart]

    Monday, January 26, 2015 7:58 PM
    Moderator
  • Sorry about that, i quoted the wrong reply.  I've deleted my post to eliminate confusion.


    Mike Crowley | MVP
    My Blog -- Planet Technologies

    Wednesday, February 25, 2015 7:40 PM
  • Wrong. There is a delete method and that does work, but you have to start Powershell "as administrator".
    Tuesday, October 06, 2015 7:33 PM
  • In my case, the use of invoke-command will create my profile on the remote workstationa again.
    Saturday, November 07, 2015 6:40 AM
  • Hi,

    I stumbled upon an other way to remove profiles by powershell.

    Try this:

    Get-CimInstance -Class Win32_UserProfile | Where-Object ( $_.Localpath -eq 'c:\users\user' ) | Remove-CimInstance

    This way you get the profile completely removed from disk, as wel from the registry and doesn't require 3th party programmes.

    Pierre

    Sunday, December 20, 2015 10:39 PM
  • That oneliner is great, but what if the profile is in use? I haven't tested profiles in use. Is there a way to tell powershell that a profile is in use, ie. "logged in"?

    Freddy

    Thursday, February 04, 2016 12:21 PM
  • That oneliner is great, but what if the profile is in use? I haven't tested profiles in use. Is there a way to tell powershell that a profile is in use, ie. "logged in"?

    Freddy


    This is a 5 year old thread.  Please open a new question with a clear question.  Your statement does not reference any code or post.

    \_(ツ)_/

    Thursday, February 04, 2016 1:39 PM
  • This works for me:

    $profiles = get-wmiobject -class win32_userprofile | 
      where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\admin") -and
      ($_.LocalPath -ne "C:\users\user"))}

    echo "profiles set to:" $profiles.LocalPath

    foreach ($profile in $Profiles) {
      echo $profile.LocalPath
      $profile.delete()
      echo "the result is $?"   # the result
    }

    • Edited by JS2010 Thursday, July 21, 2016 2:13 PM
    Wednesday, July 20, 2016 6:49 PM
  • Is there an issue? The thread is closed.  Why add a wrong answer to a 5 year od thread?

    \_(ツ)_/

    Wednesday, July 20, 2016 6:57 PM
  • Actually John is right.  The original command was fine as long as you are running it as administrator, otherwise you get the 0 arguments error.  This thread comes up in google so I'm commenting on it.


    Thursday, July 21, 2016 2:14 PM