How do I Delete User Profile

This topic contains 9 replies, has 4 voices, and was last updated by  postanote 5 days, 18 hours ago.

  • Author
    Posts
  • #85909

    Ankit Khandelwal
    Participant

    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

    Alex
    Participant

    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

    Ankit Khandelwal
    Participant

    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

    Deep
    Participant

    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

    Ankit Khandelwal
    Participant

    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

    Alex
    Participant

    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

    Ankit Khandelwal
    Participant

    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

    Alex
    Participant

    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

    Ankit Khandelwal
    Participant

    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

    postanote
    Participant

    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'

You must be logged in to reply to this topic.