Solved

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

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ON RDS SERVER 2012 CREATING A C DRIVE FOR EACH USER 3 109
Group Policy Object - Mapping DrivesI'v 5 54
Need more details 5 149
password vault 2 46
It is a common problem that often server suffers from the lack of space on system volume. Old servers or new ones from vendors come with preformatted small volume - 5-6GB in total and after installing updates or applications the free space on system…
The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

770 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