Solved

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

Posted on 2014-12-20
1
231 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 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
In this article we have discussed the manual scenarios to recover data from Windows 10 through some backup and recovery tools which are offered by it.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

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

12 Experts available now in Live!

Get 1:1 Help Now