Remove local AD user

Welcome Forums General PowerShell Q&A Remove local AD user

Viewing 3 reply threads
  • Author
    Posts
    • #188620
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      This is making me a bit bonkers because it was working and now it's not. I'm trying to remove local AD user profiles on a machine without registry hacks and the like. The script i'm using is:

      #VB Code for name entry
      Add-Type -AssemblyName Microsoft.VisualBasic
      ##get user name
      $userName = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the user name whose profile you want to delete", "Get User Name")
      $userSID = (Get-WmiObject -Class Win32_useraccount -Filter "Name = '$userName'").SID
      Write-Host $userSID
       $user = Get-WMIObject -Class Win32_UserProfile -Filter "SID = '$userSID'"
      Write-Host $user
      $user | Remove-WmiObject
      and the result is:
      Remove-WmiObject :
      At line:12 char:9
      + $user | Remove-WmiObject
      +         ~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [Remove-WmiObject], FileLoadException
      + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.RemoveWmiObject
      if I try it with delete(), i.e. $user.delete()
      I get:
      Exception calling "Delete" with "0" argument(s): ""
      At line:13 char:1
      + $user.delete()
      + ~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : DotNetMethodException
      The WMI service is running, and this is in an administrative powershell window, and I'm running it as an admin on that machine. But I've no idea why this keeps failing.
    • #188656
      Senior Moderator
      Topics: 8
      Replies: 1141
      Points: 3,928
      Helping Hand
      Rank: Community Hero

      Win32_userprofile object doesn't have a delete method according to below documentation.

      https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ee886409(v=vs.85)?redirectedfrom=MSDN#members

      Below blog post might help you

      https://adamtheautomator.com/powershell-delete-user-profile/

      • #189970
        js
        Participant
        Topics: 27
        Replies: 717
        Points: 1,886
        Helping Hand
        Rank: Community Hero

        get-wmiobject adds in the delete method. It's the same thing as piping to remove-wmiobject.

    • #189862
      Participant
      Topics: 0
      Replies: 11
      Points: 55
      Helping Hand
      Rank: Member

      `Remove-LocalGroupMember -Group "Administrators" -Member "Admin02", "MicrosoftAccount\username@Outlook.com", "AzureAD\DavidChew@contoso.com", "CONTOSO\Domain Admins"`

      I've had success with the above when using WinRM and using invoke on the local machine after entering each session.

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/remove-localgroupmember?view=powershell-5.1

      Also, you may be trying to much things...

      $userName = Read-host -Prompt "Enter the user name whose profile you want to delete"
      $userSID = (Get-WmiObject -Class Win32_useraccount -Filter "Name = '$userName'").SID
      Write-Host $userSID
      $user = Get-WMIObject -Class Win32_UserProfile -Filter "SID = '$userSID'" | Remove-WmiObject -WhatIf

      The output with the -WhatIf option...

      PS C:\> $userName = Read-host -Prompt "Enter the user name whose profile you want to delete"
      $userSID = (Get-WmiObject -Class Win32_useraccount -Filter "Name = '$userName'").SID
      Write-Host $userSID
      $user = Get-WMIObject -Class Win32_UserProfile -Filter "SID = '$userSID'" | Remove-WmiObject -WhatIf
      
      Enter the user name whose profile you want to delete: administrator
      S-1-5-21-2573404762-1233068552-0000101010-1001
      What if: Performing the operation "Remove-WmiObject" on target "\\GenericMachine\root\cimv2:Win32_UserProfile.SID="S-1-5-21-2573404762-1233068552-0000101010-1001"".
      
      PS C:\>
    • #191581
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Remove-CIMInstance is pretty glitchy. It keeps giving me:

       

       Remove-CimInstance : The process cannot access the file because it is being used by another process.  

      which requires a restart to clear.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.