Welcome Forums General PowerShell Q&A How do I Delete User Profile

Viewing 10 reply threads
  • Author
    Posts
    • #85909
      Participant
      Topics: 3
      Replies: 4
      Points: 0
      Rank: Member

      Hi There,

      Please help me get the correct code for deleting the User local profile with specific naming convention .
      Ex. I want to delete user names logged in with abc_xyz while abc_ is constant and xyz will have different names of users.

      My code is :
      Get-WMIObject Win32_UserProfile |where-object {$_.localpath -like “c:\users\abc_*”} 

      But i do not get it right also i am not able to delete these profiles.
      Please help. !

    • #85949
      Participant
      Topics: 3
      Replies: 36
      Points: 1
      Rank: Member

      Your query just lists the profiles on that machine.
      The way I remove profiles is with:

      $username = [yourusername]
      $localProfilePath = "C:\\Users\\$Username"
      $WMIQuery = "SELECT * FROM Win32_UserProfile WHERE localpath = '$localProfilePath'"
      $profile = Get-WmiObject -Query $WMIQuery -ComputerName $Computer 
      Remove-WmiObject -InputObject $profile  
      
    • #85955
      Participant
      Topics: 3
      Replies: 4
      Points: 0
      Rank: Member

      Thank you,

      But I want to delete user names logged in with abc_xyz while abc_ is constant and xyz will have different names of users.

      Example : abc_ankit
      abc_gupta500
      abc_khand590

      How do I get that working in it . ?

    • #85957
      Participant
      Topics: 8
      Replies: 20
      Points: 0
      Rank: Member

      Try this…

      foreach($user In “abc”,”xyz”)
      {
      $username = “abc_$user”
      Write-host “$username”
      $localProfilePath = “C:\\Users\\$Username”
      $WMIQuery = “SELECT * FROM Win32_UserProfile WHERE localpath = ‘$localProfilePath'”
      $profile = Get-WmiObject -Query $WMIQuery -ComputerName $Computer
      Remove-WmiObject -InputObject $profile
      }

    • #85960
      Participant
      Topics: 3
      Replies: 4
      Points: 0
      Rank: Member

      OK But how do i get all the logged in user details in $user .
      I mean to say There are many users logged in with ID similar to abc_xyz123 also with other names such as doma500 or sa_account

      Among all I need to filter only users whose profile begins with “abc_” and delete it .
      Please get me some help please.

    • #85984
      Participant
      Topics: 3
      Replies: 36
      Points: 1
      Rank: Member

      You would need to loop through the profiles with something like:

      $profiles = Get-WmiObject Win32_UserProfile -ComputerName $Computer | where {$_.localpath -like "C:\Users\abc_*"}
      foreach($P in $profile){
          Remove-WmiObject -InputObject $p
      }

      This would remove any profile that starts with abc_
      **I have not tested this.

    • #85997
      Participant
      Topics: 3
      Replies: 4
      Points: 0
      Rank: Member

      It throws an error :

      Remove-WmiObject : Cannot bind parameter ‘InputObject’. Cannot convert value “C
      :\Users\Microsoft.PowerShellISE_
      profile.ps1” to type “System.Management.ManagementObject”. Error: “Invalid para
      meter ”
      At C:\Users\Untitled2.ps1:4 char:31
      + Remove-WmiObject -InputObject <<<< $p
      + CategoryInfo : InvalidArgument: (:) [Remove-WmiObject], Paramet
      erBindingException
      + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh
      ell.Commands.RemoveWmiObject

      Please help me on this

    • #86048
      Participant
      Topics: 3
      Replies: 36
      Points: 1
      Rank: Member

      Hi Ankit,

      If you just tried to copy/paste that code and ran it, then please be really careful.
      I think it is not working is because I left the -computername parameter in the get-wmiobject.

      Running code you do not fully understand what its doing can cause major issues. So please in the future, read through the code. Try and understand what its doing, and evaluate the risk.
      If you don’t understand it, then ask us here. I am sure people here will be more than happy to help you understand the code.

    • #86054
      Participant
      Topics: 3
      Replies: 4
      Points: 0
      Rank: Member

      yes, that is right.

      I am very new to PSS and willing to learn more .
      Can you please correct the code and help me get it through Please ?

    • #86371
      Participant
      Topics: 2
      Replies: 1008
      Points: 2,056
      Helping Hand
      Rank: Community Hero

      There are pre-built scripts via MS website and this site as well as others which cover this use case.

      However, I cannot over emphasize to you enough what has been already said. Don’t just arbitrarily run anyone’s code you do not fully understand in a production environment, and even in test be cautious and test on systems which you don’t mind destroying or having issues on. Meaning ones you may have to rebuild if you get it wrong.

      You should really take a quick PowerShell online course using Microsoft Virtual Academy, before doing any real work with PowerShell, especially this sort of thing, so, that you have a real baseline understanding. It’s good to experiment, but experiment in a safe zone.

      # Simple search for training using your favorite search engine
      ‘Microsoft virtual academy’ PowerShell
      ‘youtube.com/results?search_query=powershell+ise+scripting+for+beginners’
      ‘youtube.com/results?search_query=powershell+for+beginners+’

      # Delete Unused user Profiles on local machine (PowerShell)
      ‘gallery.technet.microsoft.com/scriptcenter/Delete-Unused-user-4047f41d’

      # Script Delete user profiles over multiple servers v2
      ‘gallery.technet.microsoft.com/scriptcenter/Delete-user-profiles-over-05348eef’

      # Other resoruces explaining the same use case.
      ‘learn-powershell.net/2011/01/27/use-powershell-to-remove-local-profiles’
      ‘powershell.org/forums/topic/deleting-old-user-profiles’
      ‘techibee.com/powershell/powershell-script-to-delete-windows-user-profiles-on-windows-7windows-2008-r2/1556’
      ‘bisbd.blogspot.com/2014/10/powershell-managing-user-profiles-on.html’

    • #113659
      vjs
      Participant
      Topics: 0
      Replies: 4
      Points: 2
      Rank: Member

      #One of the ways profiles older than two days were deleted easily by using below script.

      $d = (get-date).AddDays(-2)
      $prof = @(ls c:\users -Exclude “Administrator”)
      $tobedeleted=@()
      foreach($i in $prof){
      if($i.lastwritetime -lt $d){
      $tobedeleted += $i }
      }
      $tobedeleted

      foreach($p in $tobedeleted){
      cmd.exe /c “rmdir /s /q $p”
      }

Viewing 10 reply threads
  • The topic ‘How do I Delete User Profile’ is closed to new replies.