Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete local windows profile with PowerShell

Tags:

powershell

wmi

I am trying to write a script that will delete the local profile of a test account. I am using the following line to return the SID of any account that starts with "test-"

PowerShell: $UserSID = (Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '*\test-*'}).SID

Once I had the SID I used wmic to do the deletion but, I am not sure how to translate that code into PowerShell.

WMIC:wmic /node:"localhost" path win32_UserProfile where Sid="%%b" Delete

like image 401
pizzim13 Avatar asked Dec 21 '22 23:12

pizzim13


1 Answers

I was thinking this would work, but I don't find a delete method on the Win32_UserProfile class

$UserSID = (Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '*\test-*'}).SID
(gwmi -class Win32_UserProfile -filter "SID='$UserSID'").Delete()
like image 181
Mike Shepard Avatar answered Jan 21 '23 01:01

Mike Shepard