Jump to content

Recommended Posts

Posted (edited)

Hi,

Since migrating from windows 7 to 10 and now 11 we have noticed that local profile sizes seem to have increased! More precisely the size of user profiles on student machines seem to have increased compared to windows 7. We often seem to be deleting profiles to obtain some space on the C drive! I have edited the attached PowerShell script which deletes the user profiles from the C drive and from the profile list within the advanced tab in system properties. I just don't know why the output when the script is running displays the first line that the user profile is being deleted and then the following line displays 'WARNING: Failed to delete user profile for' then displays the users name??

 

The IT Manager has tried using the GPO that deletes user profiles but he said that it didn't work fully. He has also tested Delprof2 but it apparently didn't fully remove the profiles.

 

thanks in advance! 😁

PowerShell Output for User Deletion Script.PNG

Use Profile Deletion Script.ps

Edited by cheaptonersucks
Posted (edited)

Try the scripts towards the end of this article: 

 

How to Delete Old User Profiles in Windows | Windows OS Hub

 

The key thing compared to yours is that assessing the last used date for user profiles is much more complex that it was when the gpo evaluation rules were written for Windows 2000 through 7/8. Though I thought Microsoft had updated them to reflect how profiles work around the 22h2 release, but perhaps not. I think we have a customised version that uses CIM modules rather than the depreciated WMI ones. Not sure whether this makes and difference today, but it might in the future when the wmi cmdlets are removed.

 

By the way, a better way is to manage local profiles is in the shared device profile from Intune (if its available for you). 

 

Finally, if your have onedrive in play and the profile contains un-sync'd items  we have found that the folders containing the unsync'd items are left behind, as a last ditch effort by the operating system to prevent you from forcing data loss on the user.

Edit because I linked to the wrong script -should have realised and things were more different that I was expecting. 

Edited by psydii
  • Like 1
Posted

 

Slightly more simple version that I use to Delete profiles, it removes whatever is not logged in or specifically excluded. 

$LoggedInUsers =  (Get-WMIObject -ClassName Win32_ComputerSystem).Username.substring(4) 
$Dirs = Get-ChildItem c:\users\ -Exclude "Public", $LoggedInUsers,"Default" | select -ExpandProperty Name
get-volume
foreach ($dir in $dirs) {
write-host "Trying to remove profile with path of $dir" -forgroundcolor green
Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $dir } | Remove-CimInstance
write-host "Removable attempt complete" -forgroundcolor blue
}

 

Sometimes people overcomplicate PowerShell scripts and it causes issues 

Also if your using newer versions of PowerShell wmi will no longer work per what psydii was saying 

  • Like 1
Posted

I had issues with DelProf and the GPO as well, so ended up scripting a version myself which I then run through ConfigMgr as a baseline detection, then remediation script but you can simplify it down more than that if needed.

 

I find that the last login details from CIM and WMI and even the folder dates are useless, so I query all profiles with CIM, then grab a list of everyone who's logged on in the last 14 days from the security log with Get-WinEvent. Compare to see who hasn't been on in that time and then remove the profiles with CIM as well. I can share the script if needed but you'd need to adapt it to your own use.

  • Like 1
Posted

Thanks for your responses! Bit snowed under as I am the only IT tech in large secondary, so apologies for the delay in replying! I am pretty new to PowerShell and every script I have tested so far, I get red output when I run them!? 🤣

Posted
11 minutes ago, cheaptonersucks said:

Thanks for your responses! Bit snowed under as I am the only IT tech in large secondary, so apologies for the delay in replying! I am pretty new to PowerShell and every script I have tested so far, I get red output when I run them!? 🤣

PowerShell normally gives decent error messages

Care to share? 

Posted

Every PowerShell user deletion script that I have ever tried to run error's with red text!! 🤣 What am I doing wrong!? I always reboot the machine and run the scrips as Admin. I just tried running your PS script Machy and here is s screen shot of the errors after I ran it. It's driving me nuts as every PS script that I run outputs red text which I assume are errors? I just need a PS script that deletes all traces of the user profile including registry entries and user profile folder on the c drive... That don't error with red text! 😂

Screenshot 2025-07-17 101548.png

Posted
2 minutes ago, cheaptonersucks said:

Every PowerShell user deletion script that I have ever tried to run error's with red text!! 🤣 What am I doing wrong!? I always reboot the machine and run the scrips as Admin. I just tried running your PS script Machy and here is s screen shot of the errors after I ran it. It's driving me nuts as every PS script that I run outputs red text which I assume are errors? I just need a PS script that deletes all traces of the user profile including registry entries and user profile folder on the c drive... That don't error with red text! 😂

Screenshot 2025-07-17 101548.png

My script is Lazy so it doesn't do error checking

 

In that case its because "administrator" is logged in (i presume its the account you are running the script as)

Therefore its trying to delete a live account.

Which means for some reason its not picking up admin being logged in (ill look at why) 

 

swap 

$Dirs = Get-ChildItem c:\users\ -Exclude "Public", $LoggedInUsers,"Default" | select -ExpandProperty Name

with 

$Dirs = Get-ChildItem c:\users\ -Exclude "Public", $LoggedInUsers,"Default","administrator"| select -ExpandProperty Name

and run it again 

 

 

  • Thanks 1
Posted

You can age out local profiles in GPO (mine are set to 14 days) also the profiles get massive if you are using MSIX installers. (as each pupil get a copy of the entire program)

 

 

  • Thanks 1
Posted

Thanks Chazzy2501. the IT manager said that he has tried to use GPO for deleting profiles but he said that there were issues. I am not sure exactly what didn't work fully though. I know what you mean about massive profiles. I saw a Y8 profile a while ago which was just over 10GB!!!! 😱

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...