petben Posted March 28, 2019 Posted March 28, 2019 Hi, I got this profiles delete script from EduGeek 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 powershell error attached that appears many many times at startup..
SparkySX Posted March 28, 2019 Posted March 28, 2019 Can't help you on the error, but, have you tried Delprof2? I've used it at logoff or machine startup and it seems to handle errors quite well https://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool/
ChrisMiles Posted March 29, 2019 Posted March 29, 2019 Some problems here, you can't delete profiles using Remove-WmiObject, you need to call the delete method. Get-WMIObject -Class Win32_UserProfile | Where-Object { $_.Special -ine "True" -and $_.LocalPath -ine "C:\Users\Administrator" -and $_.LocalPath -ine "C:\Users\Public" -and $_.LocalPath -ine "C:\Users\Default"} | ForEach-Object { $_.Delete() } If you want something more complete, I wrote a profile management routine in our startup script that removes student profiles, mandatory, corrupt and temporary profiles, but leaves staff profiles intact. It also uses a AD variable to allow you to purge specific profiles across the network for when roaming profiles need resetting. You can find it here: http://www.edugeek.net/forums/windows-10/204731-script-deleting-user-profiles-folders.html#post1748036
petben Posted March 29, 2019 Author Posted March 29, 2019 Thanks Chris I get: Exception calling "Delete" with "0" argument(s): "" At line:1 char:304 + ... _.LocalPath -ine "C:\Users\Default"} | ForEach-Object { $_.Delete() } + ~~~~~~~~~~~ + CategoryInfo : NotSpecified: ( [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException when running that command in the code block of your message.
ChrisMiles Posted March 29, 2019 Posted March 29, 2019 Thanks Chris I get: Exception calling "Delete" with "0" argument(s): "" At line:1 char:304 + ... _.LocalPath -ine "C:\Users\Default"} | ForEach-Object { $_.Delete() } + ~~~~~~~~~~~ + CategoryInfo : NotSpecified: ( [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException when running that command in the code block of your message. That could happen if you are not running PowerShell or the ISE as Administrator, deleting profiles requires elevated permissions. It may also be trying to delete a profile that is in use (yours for example) so you may want to add a check for that: Get-WMIObject -Class Win32_UserProfile | Where-Object { $_.Loaded -ine "True" -and $_.Special -ine "True" -and $_.LocalPath -ine "C:\Users\Administrator" -and $_.LocalPath -ine "C:\Users\Public" -and $_.LocalPath -ine "C:\Users\Default"} | ForEach-Object { $_.Delete() }
Patrick Posted March 29, 2019 Posted March 29, 2019 If you make the users members of Domain Guests in AD their profile is automatically cleared at log off, that's what i do for the students and it's worked well for me.
petben Posted March 29, 2019 Author Posted March 29, 2019 Group membership is a nice idea, it seems to remove the reg entry but leaves an (empty) folder in C:\users called 'username'.
ChrisMiles Posted March 29, 2019 Posted March 29, 2019 The deletion is done by windows, if it leaves a folder it'll be because theres open file locks. Do profile cleaning after a fresh reboot (in the startup script ideally) to avoid this.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now