none
Powershell - Deleting user profiles from a Local PC RRS feed

  • Question

  • Hello all,

      I am new to PowerShell but have been working on a script for my team to load on to computers and delete all local profiles. It seems simple enough and I have looked and looked online for advice, not being able to figure it out I thought I would reach out to you. below is the current script I am using.

    $computer = $ENV:COMPUTERNAME #set computer variable to local host

    $Profiles = get-WMIobject WIN32_UserProfile -computername $computer #pull profiles from PC

    foreach ($profile in $profiles) #set command for each profile

    {$profile.delete()} #delete each profile

    My error is Exception calling argument "delete" with Argument "0"

    $profile.delete() is underlined.

    Any help would be appreciated.

    Rich

    Tuesday, June 2, 2015 5:16 PM

Answers

All replies

  • You cannot delete all profiles.  Only user profiles and only ones that are not in use.


    \_(ツ)_/

    Tuesday, June 2, 2015 6:11 PM
  • Then I would have to delete the profiles one by one? or is there a way to delete all profiles except the ones that are in use or ones like "default" or "local". Some of our computers have 30+ unused profiles and we have hundreds of computer. There has to be a simpler way.
    Tuesday, June 2, 2015 6:34 PM
  • I recommend the delprof2 tool, which lets you specify a cutoff time and runs from the command line.


    -- Bill Stewart [Bill_Stewart]


    Tuesday, June 2, 2015 7:08 PM
    Moderator
  • FYI that tool is free. My response is not advertising.

    -- Bill Stewart [Bill_Stewart]


    Tuesday, June 2, 2015 7:19 PM
    Moderator
  • All can be done by WMI but you need to learn how to use it.

    You have to filter out "special" accounts and filter on the last da\te the profile was used.

    Of course this is exactly what DelProf2 does and you don't have to script it.


    \_(ツ)_/

    Tuesday, June 2, 2015 7:43 PM
  • I have been able to filter out accounts the have the SID 18,19or 20. the list only brings up accounts with SID 21. I cant find the command to delete the profile.  I have tried $profile.delete() and remove-WMIobject and had no luck.
    Tuesday, June 2, 2015 8:24 PM
  • You have to filter special accounts.  If you do not know WMI or PowerShelkl you would be better off with DelProf2.

     gwmi win32_userprofile -filter 'Special=False'|select SID,loaded,lastusetime,Status

    When using WMI you need to study each class.  You canot guess if you want to become a computer tech. Guessing is for gamblers and children.


    \_(ツ)_/


    • Edited by jrv Tuesday, June 2, 2015 8:29 PM
    Tuesday, June 2, 2015 8:28 PM