Solved

Profile Fragments after Removal (Windows 7)

Posted on 2011-03-15
8
723 Views
Last Modified: 2012-05-11
I operate a computer lab that is open to general use on a campus. Within a week or so we can collect about 100-200 user profiles on the computer.

I've been using a WMI Powershell script to clean the profiles, and it seems to work ok, but now I've noticed some profiles are being left with a single empty folder inside them. The folder is Appdata, and inside it is usually an empty start menu folder or empty Windows media player folder.

I've tried using the Group Policy to remove profiles but it doesn't seem to work well, even after applying a patch to prevent Anti-virus from modifying the last accessed date.

What am I doing wrong? or is there a better way?

I set the script to run at 5;45am on Sunday and I set the BIOS to wake the computer at 5:30am daily. The script is set to run with no one logged in as "SYSTEM" with full permissions, but the scheduled task never runs, I have to run it by hand, and that's a pain on 12 computers.

I really don't understand why Microsoft made automatic profile removal so very hard.

I appreciate any help I can get.

# Powershell Script to delete all user profiles in your computer Except the once that you specify in the Exception List
# Running Syntax: .\ws50_del_unused_profiles.ps1 
# This script will delete the profile completely including the SID, Registery key, in the user profile list,
# and also from C:\Users.
# This script will not delete the user profile through which you are currently logged in

function Get-Profiles
{   
	<#
		.Synopsis
		Gets a list of user profiles on a computer.
	
		.Description
		This command gets a list of user priles on a computer. The info is pipable and can be used to do other useful tasks.
		
		.Parameter computer
		The computer on which you wish to recieve profiles. (defaults to localhost)
	
		.Example
		Get-Profiles -comptuer comp1
		Gets all of the profiles from comp1
		
		.Example
		Get-Content .\computers.txt | Get-Profiles
		Returns all of the profiles for the computers listed in computers.txt	
		
	#>
	[CmdletBinding()]
    	param ([parameter(ValueFromPipeLine=$true)][String]$computer = "localhost")	
	process {
		$ErrorActionPreference = "SilentlyContinue"
        	# Check for pipe input
		if ($_.Name) { $computer = $_.Name }
        	elseif ($_) { $computer = $_ }
       		else {  }

		$profiles=$null
		# Get the userprofile list and then filter out the built-in accounts
		if ($computer) {
			#$profiles = Get-WmiObject win32_userprofile -computerName $computer | ?{$_.SID -like "s-1-5-21*"}
            #if (!$?) { Write-Warning "Unable to communicate with - $computer" }
            
            $profiles = Get-WmiObject win32_userprofile -computerName $computer			
		}
		else {
			Write-Warning "Unable to communicate with specified host."
		}
		
		if($profiles.count -gt 0 -or ($profiles -and ($profiles.GetType()).Name -eq "ManagementObject")) {
			# Loop through the list of profiles
			foreach ($profile in $profiles) {
				Write-Verbose ("Reading profile for SID " + $profile.SID + " on $computer")
				$user = $null
				$objUser = $null
				#Create output objects
				$Output = New-Object PSObject
				# create a new secuity identifier object
				$ObjSID = New-Object System.Security.Principal.SecurityIdentifier($profile.SID)
				# Try to link the user SID to an actual user object (can fail for local accounts on remote machines, 
				#  or the user no long exists but the profile still remains)
				Try { 
					$objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
				}
				catch { 
					$user = "ERROR: Not Readable"
				}
				
				if ($objUser.Value) { $user = $objUser.Value }
				
				$Output | Add-Member NoteProperty Computer $computer
				$Output | Add-Member NoteProperty Username $user                 
				#$Output | Add-Member NoteProperty SID $profile.SID
				#$Output | Add-Member NoteProperty Path $profile.LocalPath
				#$Output | Add-Member NoteProperty Profile $profile
                       
				Write-Output $Output
			}
		}
	}
}

function Remove-Profiles(){    
    
	# Defining Exception List
      $exceptionList = New-Object System.Collections.ArrayList
      $result =$exceptionList.Add("Administrator")
      $result =$exceptionList.Add("Default User")
      $result =$exceptionList.Add("Default")
      $result =$exceptionList.Add("Default Profile")
      $result =$exceptionList.Add("All Users")
      $result =$exceptionList.Add("Local Service")
      $result =$exceptionList.Add("Network Service")
      $result =$exceptionList.Add("csuser")      
      $result =$exceptionList.Add("clsmach")
      $result =$exceptionList.Add("sam")
      $result =$exceptionList.Add("lsreer0")
      $result =$exceptionList.Add("cbyun0")
      $result =$exceptionList.Add("SYSTEM")
      $result =$exceptionList.Add("Public")
      
      $chkDelete = 1;
      
      if($chkDelete -eq 1){
        $profiles = Get-WmiObject win32_userprofile -computerName "localhost"
        
        foreach($delProfile in $profiles){
            $ObjSID = New-Object System.Security.Principal.SecurityIdentifier($delProfile.SID)
            $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
            
            $user = $objUser.value
            $userName = $user.split("\")
            
            $chkValue = 1
            foreach($exceptionName in $exceptionList){
                if($exceptionName -eq $userName[1]){
                    $chkValue=0
                }                
            }
            
            if($chkValue){
                 $delUserProfile = Get-WmiObject -query ("select * from win32_userprofile where SID='" + $delProfile.SID + "'") -computer "localhost"                             
                
                try { 
                    echo $delUserProfile.Delete() 
                    echo " All User Profile Deleted Successfully as if you would do it using Wizard!"
                }catch { echo "Error!!: Either $uprofile is logged in currently or User profile doesn't Exists" }            
            }
            
              
        }       
      }
}

Get-Profiles($env:computername)
Remove-Profiles
Get-Profiles($env:computername)

Open in new window

Task-Remover.xml
Clipboard01.jpg
0
Comment
Question by:TowsonStaff
  • 6
  • 2
8 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
You might want to  use group policy instead.. run gpedit.msc
Computer Configuration / Administrative Templates / System / User Profiles / Delete user profiles older than a specified number of days on system restart. Select Enable and then select the day you would like to delete or remove the profile.

0
 

Author Comment

by:TowsonStaff
Comment Utility
I activated that policy as well but we had a lot of problems with that policy that made us go to this new script mostly related to profiles that weren't in use being modified by a virus scanner. I applied the patch that Microsoft says will prevent that so let's hope.

I'm trying it again with the hopes things will change.
0
 

Author Comment

by:TowsonStaff
Comment Utility
I set the process to delete profiles older than one day, and ran gpupdate.

I still see profiles from back in February on the machines.
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
hmmmm... have you looked at programs i.e. deepfreeze  or only having 1 user profile that brings up a virtual machine that will revert to original on reboot?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:TowsonStaff
Comment Utility
I'd love to be able to do the Virtual machine thing with differencing disks, but it's far beyond the ability of what we have here. I wanted to buy a premade program for profile removal, but they won't let me.

I applied the Group Policy above, now when I run GPupdate or gpupdate /force, I get this error

Registry value "%windir%\SoftwareDistribution\Datastore\Logs\Res*.log" is of unexpected type.

or

"%windir%\Security\Database\*.jr" is of unexpected type.

0
 

Author Comment

by:TowsonStaff
Comment Utility
I tried using the GPO profile removal tool, and it removed 3/4 profiles. But it still left profile folders with nothing in them.

Problem is because the folder is there, windows creates a new profile called gprimros.towsonU

If I let it go, it will start making folders

gprimros.towsonu.000
gprimros.towsonu.001

Obviously this is not an acceptable solution. I find it hard to believe there is no way to remove user profiles effectively.
0
 

Accepted Solution

by:
TowsonStaff earned 0 total points
Comment Utility
I found a solution:

Place the groups you desire to have their profiles deleted into the Guests group.

All user profiles that are connected with groups in the Guests folder will have their profile deleted at log-off.

Profiles that are in the administrators group as well are exempted.
0
 

Author Closing Comment

by:TowsonStaff
Comment Utility
I figured it out myself.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I found out last night windows update has a problem regarding 4 latest updates that fail.  The way to get all 4 them installed is install sp1 first and restart then one by one with a resart in between as they fail every time if all the four updates …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now