Hello again! Let me tell you something about the user profiles and how annoying can it be once its piled up in a server, where you have to loose Server or Clients space for staffs who basically left the organization. I had to face the same situation in my organization where majority of the servers are filled with user profile which cost the space on server an avg of 7 – 13 Gigs. Once i figured that one server had consumed this much of data been stored since past 5 years on the server I started to run this simple script on majority of the server. The result was surprising where i manged to clear storage around 200GB on all servers that i ran this script. How simply these things are missed and eating up free spaces.
Removing Legacy profiles are good in general Housekeeping obviously do check with Manager or team lead before proceeding with these sort of housekeeping for proper approval and Change raised if its required (Most of the Org will require a Change request for this).

Before removing the profile obviously have a report of the finding. the script also puts all the finding into a an Hash Table which is much more easier to read as below. All you have to do is Run the following command to see it in a grid view which is pre-built in Powershell. $Report | out-gridview

Once you have the Data in a readable format its pretty easy to do the rest which is making decision based on it. Now we have the data all we need to do is take the decision whether which date backwards you are going to delete the profiles. Lets say anything below 2015.
Just added a quick script to find out how many users profiles are there that are last access date is less than 2015 – found that are 36 profiles.

What if there are Accounts which are accessed before 2015 and some of the account needs to be retained without deleting like Administrator, Public or any other specific profiles. then its just another matter if condition.

Now you have all the necessary details to add this one command which will do the eliminate the user profiles. get-wmiobject -class win32_userprofile -computername $machinename | ? {$_.LocalPath -eq $semiReport.ProfileLocation} | Remove-wmiobject
The removal part is commented to avoid removing profile mistakenly.
Revealing the full script here. you can pass one machine a time or you can pass a list as well.
$machinename = Read-Host "Please Type the host Name "
#$machinenames = Get-Content -Path "Filepath with machine name"
$existingusers = Get-ChildItem "\\$machinename\c$\users" | select name
$Report = @()
#if you are using a list please change the foreach $existingusers to $machinesNames
foreach($username in $existingusers){
$checkforsrv =$username.Name.Contains("Adminsitrator")
$checkforpublic = $username.Name.Contains("Public")
$checkforDefault = $username.Name.Contains("Default")
if($checkforsrv -eq "True"){
#do nothing.
}elseif($checkforpublic -eq "True"){
#do nothing
}elseif($checkforDefault -eq "True"){
#do nothing
}else{
$name = $username.Name
$SemiReport = New-Object -TypeName psobject -Property @{
ServerName = $machinename
ProfileLocation = "\\$machinename\C$\users\$name"
UserName = $username.Name
"Size(in MB)" = "{0:N2}" -f ((Get-ChildItem -Path "\\$machinename\C$\Users\$name" -Recurse| Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum /1MB)
LastAccessDate = (Get-ItemProperty -Path "\\$machinename\c$\users\$name" | select LastAccessTime).LastAccessTime
}
$Report += $SemiReport | Select-Object ServerName,ProfileLocation,Username,"Size(in MB)",LastAccessdate
}
}
$count = 0
foreach($entry in $Report){
if($entry.LastAccessDate -lt "01-01-2015"){
#Get-WmiObject -class Win32_UserProfile -ComputerName "$machinename" | ? {$_.LocalPath -eq "C:\Users\$name"} | Remove-WmiObject
$count = $count + 1
}else{
}
}
$count
Caution: Please be cautious about the environment always test on test environment and obtain necessary approvals.

Very nice indeed I guess we can use Lansweeper here to find the Computers which have run out of room and use your script remotely, at the moment I use Delprof2.exe which i can run remotely.
LikeLike