Delete a local Windows profile using PowerShell

I am trying to write a script that will delete the local profile of the test account. I use 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 remove it, but I'm not sure how to translate this code into PowerShell.

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

+3
source share
5 answers

I thought this would work, but I did not find the delete method in the Win32_UserProfile class

$UserSID = (Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '*\test-*'}).SID
(gwmi -class Win32_UserProfile -filter "SID='$UserSID'").Delete()
+4
source

You can also just call the Delete method directly in one expression:

(Get-WmiObject Win32_UserProfile | Where {$_.LocalPath -like '*\test-*'}).Delete()
+3
source

Another reason for getting an exception that throws a β€œDelete” with an argument of β€œ0” is because the user you are trying to delete is currently logged in. Run it and try again.

+2
source
Get-WmiObject Win32_UserProfile -Filter "RoamingConfigured = 'True'" | Remove-WmiObject

True- Roaming profile
False- Local profile

+1
source

I solved this problem by opening Powershell as administrator (right click, Run as administrator).

0
source

Source: https://habr.com/ru/post/1765716/


All Articles