Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever stumbled upon a software that is so great that you just love? It happened to me. Love at first sight. Filezilla Server.   Ok its not the most advanced ftp server I've came across. But its a fairly simple piece of software to get the …
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now