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