Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to purge old roaming profile and redirected mydocs/desktop data for inactive users

Posted on 2014-12-20
1
Medium Priority
?
260 Views
Last Modified: 2014-12-25
On our file server we have a bunch of old profiles and redirected mydocs/desktop folders for users that are no longer active.  Is there an easy way to determine which profile folders have not been used in 'x' days so that we can purge them from primary storage?  We could probably pull a list of AD accounts (both active and disabled) that haven't logged in in 'x' days and cross-reference to the folders on the file server but I'm looking for something less manual.

Thanks!
0
Comment
Question by:River City Bank
1 Comment
 
LVL 84

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 total points
ID: 40511373
Powershell Script save as "Get-DeleteProfiles.ps1"
Function Get-DeleteProfiles
{
<#
.Synopsis
   Find inactive users and delete TS Profiles
.DESCRIPTION
   This Script will select Inactive Users from Active Directory and
   delete the profile from the computer
   Modify $daysinactive, $Domain 
.EXAMPLE
   Get-DeleteProfiles.ps1 Computername
.EXAMPLE
   Get-DeleteProfiles.ps1
    Uses Current Computer by Default
#>
[cmdletbinding()] 
param( 
 [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] 
 [string[]]$ComputerName = $env:computername 
  ) 
Begin {} 
Process { 
 
import-module activedirectory 
$domain = "domain.mydom.com" 
$DaysInactive = 90 
$time = (Get-Date).Adddays(-($DaysInactive))
 
# Get all AD User with lastLogonTimestamp less than our time and set to enable
$usernames = Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp |  select-object Name
 
Write-Verbose ("Working on '{0}'",$ComputerName)
if(Test-Connection -ComputerName $ComputerName -Count 1 -ea 0) 
     { 
    foreach($username in $username)
        {
        $Profiles = Get-WmiObject -Class Win32_UserProfile -Computer $ComputerName -ea 0 
         foreach ($profile in $profiles) { 
         $objSID = New-Object System.Security.Principal.SecurityIdentifier($profile.sid) 
         $objuser = $objsid.Translate([System.Security.Principal.NTAccount]) 
         $profilename = $objuser.value.split("\")[1] 
         if($profilename -eq $UserName)
         { 
             $profilefound = $true 
            try { 
                 $profile.delete() 
                 Write-Host "$UserName profile deleted successfully on",$ComputerName
                 } 
            catch { 
                Write-Host "Failed to delete the profile, $UserName on $ComputerName" 
                } 
        } 
    }
 
     if(!$profilefound) { 
     write-Warning "No profiles found on $ComputerName with Name $UserName" 
     } 
     
     else { 
         write-verbose "$ComputerName Not reachable" 
         } 
        }
    }
 }
end {}
}

Open in new window

sources:
http://techibee.com/powershell/powershell-script-to-delete-windows-user-profiles-on-windows-7windows-2008-r2/1556
https://gallery.technet.microsoft.com/scriptcenter/Get-Inactive-User-in-78b8db79
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

916 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