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")
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.
I’ve had success with the above when using WinRM and using invoke on the local machine after entering each session.
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:\>