Remove local AD user

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.

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/

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:\>

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

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.