Link to home
Create AccountLog in
Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

Avatar of Pete
Pete🇬🇧

Script to Delete Local Profiles
Hi, I got this profiles delete script from  to run at startup, and it works but it sometimes seems to hang deleting some profiles, reboot and those left over profiles disappear. It's all a bit wrong, lots of red Powershell warnings.

can someone please advise:

$profiles = $null
$profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\Public") -and ($_.LocalPath -ne "C:\Users\Default"))}
if ($profiles -ne $null) {
$profiles | Remove-WmiObject
}

Exit

Open in new window


powershell error attached that appears many many times at startup..
User generated imageUser generated image

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of AlexAlex🇬🇧

What is it you're trying to do? Delete roaming profiles?

Thanks
Alex

Avatar of PetePete🇬🇧

ASKER

sorry, ive changed the title, that was an error.

Delete Local Profiles - folders in C:\Users etc.

Avatar of AlexAlex🇬🇧

Ok so only local profiles.

Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-5))} | Remove-WmiObject

Open in new window


That'll do it, you'd be better off using group policy though

https://social.technet.microsoft.com/wiki/contents/articles/28647.how-to-automatically-delete-user-profiles-older-than-a-certain-number-of-days-using-group-policy.aspx

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of yo_beeyo_bee🇺🇸

I do not have a test machine to run this against, but it seems like it will work.

I am using Alias (Shorthand)
GWMI -class Win32_UserProfile | ? {!$_.special -and $_.localpath -notmatch "administrator"} | Remove-WMIObject -whatif

Open in new window


Avatar of PetePete🇬🇧

ASKER

OK trying:
Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator.sls") -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\Public") -and ($_.LocalPath -ne "C:\Users\Default"))} | Remove-WMIObject

Open in new window


errors:

Remove-WMIObject :
At line:1 char:275
+ ... lic") -and ($_.LocalPath -ne "C:\Users\Default"))} | Remove-WMIObject
+                                                          ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Remove-WmiObject], COMException
    + FullyQualifiedErrorId : RemoveWMICOMException,Microsoft.PowerShell.Commands.RemoveWmiObject
 
Remove-WMIObject :
At line:1 char:275
+ ... lic") -and ($_.LocalPath -ne "C:\Users\Default"))} | Remove-WMIObject
+                                                          ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Remove-WmiObject], COMException
    + FullyQualifiedErrorId : RemoveWMICOMException,Microsoft.PowerShell.Commands.RemoveWmiObject
 
Remove-WMIObject :
At line:1 char:275
+ ... lic") -and ($_.LocalPath -ne "C:\Users\Default"))} | Remove-WMIObject
+                                                          ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Remove-WmiObject], COMException
    + FullyQualifiedErrorId : RemoveWMICOMException,Microsoft.PowerShell.Commands.RemoveWmiObject
 
Remove-WMIObject :
At line:1 char:275
+ ... lic") -and ($_.LocalPath -ne "C:\Users\Default"))} | Remove-WMIObject
+                                                          ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Remove-WmiObject], COMException
    + FullyQualifiedErrorId : RemoveWMICOMException,Microsoft.PowerShell.Commands.RemoveWmiObject
 
Remove-WMIObject :
At line:1 char:275
+ ... lic") -and ($_.LocalPath -ne "C:\Users\Default"))} | Remove-WMIObject
+                                                          ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Remove-WmiObject], COMException
    + FullyQualifiedErrorId : RemoveWMICOMException,Microsoft.PowerShell.Commands.RemoveWmiObject

Avatar of yo_beeyo_bee🇺🇸

Are you running powershell as a admin?

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of PetePete🇬🇧

ASKER

OK, it now runs, no error and immediately returns to the powershell prompt. no folders get removed., there are 50 + to remove.

Avatar of footechfootech🇺🇸

If you're having trouble with Remove-WmiObject, I would try the Delete() method.  This is what I've used in the past.
Get-WMIObject -class Win32_UserProfile |
 Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\Public") -and ($_.LocalPath -ne "C:\Users\Default"))} |
 ForEach-Object { $_.Delete() }

Open in new window


Avatar of yo_beeyo_bee🇺🇸

Is this a terminal server?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of PetePete🇬🇧

ASKER

No, these are win 10 workstations

Avatar of yo_beeyo_bee🇺🇸

Wow.  With 50+ profiles.

Avatar of PetePete🇬🇧

ASKER

Profiles get created when a new user logs in. These are open access IT rooms in a school, potentially 1500 users.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of serialbandserialband🇺🇦

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
Windows OS

Windows OS

--

Questions

--

Followers

Top Experts

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.