3

I want to free up some C Drive space on my servers by removing user profiles that from C:\users who haven't logged into the server in the last 6 months. I connect to the servers using PowerShell Cim commands.

So far I have only found the Get-CimInstance -CimSession $CimSession -ClassName Win32_UserProfile command that will list users profiles but it doesn't list the last logon time for each user. Is there another command that can be used to list UserProfiles with LastLogon? Once I have that list I want to delete any profile that hasn't logged into the server in the last 6 months.

Keith
  • 689
  • 10
  • 27
  • [`Win32_NetworkLoginProfile`](https://learn.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-networkloginprofile) class includes `LastLogon` property – JosefZ Sep 19 '18 at 22:13

2 Answers2

0

How to delete user profiles older than a specified number of days in Windows

This PowerShell script sample shows how to delete user profiles older than a specified number of days.

Example 1:  

C:\Script\RemoveLocalUserProfile.ps1 -ListUnusedDay 1

Example 2: 
C:\Script\RemoveLocalUserProfile.ps1 -DeleteUnusedDay 1 -ExcludedUsers “marry” 

# Begin Script
If ($ProfileInfo -eq $null) 
{ 
    Write-Warning -Message "The item not found." 
} 
Else 
{ 
    Foreach ($RemoveProfile in $ProfileInfo) 
    { 
        #Prompt message 
        $Caption = "Remove Profile" 
        $Message = "Are you sure you want to remove profile '$($RemoveProfile.LocalPath)'?" 
        $Choices = [System.Management.Automation.Host.ChoiceDescription[]]` 
        @("&Yes", "&No") 

        [Int]$DefaultChoice = 1 

        $ChoiceRTN = $Host.UI.PromptForChoice($Caption, $Message, $Choices, $DefaultChoice) 

        Switch ($ChoiceRTN) 
        { 
            0
            { 
                Try {$RemoveProfile.Delete(); Write-Host "Delete profile '$($RemoveProfile.LocalPath)' successfully."} 
                Catch {Write-Host "Delete profile failed." -ForegroundColor Red} 
            } 
            1 {break} 
        } 
    } 
    $ProfileInfo|Select-Object @{Expression = {$_.__SERVER}; Label = "ComputerName"}, ` 
    @{Expression = {$_.ConvertToDateTime($_.LastUseTime)}; Label = "LastUseTime"},` 
    @{Name = "Action"; Expression = {If (Test-Path -Path $_.LocalPath) 
            {"Not Deleted"} 
            Else 
            {"Deleted"} 
        }
    } 
}
# End Script

Similar approaches can be see here:

https://community.spiceworks.com/how_to/124316-delete-user-profiles-with-powershell

https://www.business.com/articles/powershell-manage-user-profiles

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
postanote
  • 15,138
  • 2
  • 14
  • 25
  • This does *not* use `CIM`. – Maximilian Burszley Sep 19 '18 at 23:10
  • granted, but that last link explicitly does. My response is all about, if one this is not working, one has options. – postanote Sep 19 '18 at 23:49
  • The last link explicitly does *not*. It uses the command once, then abandons it for `Get-WmiObject` – Maximilian Burszley Sep 20 '18 at 00:41
  • The point is the OP said what they "found so far", not any finalized thing to satisfy the use case. Again, as you know, and as you have pontificated on this very site In several of your replies, there other ways to do things with PowerShell if you are not having success with X or Y. Even is the OP choose to go CIM, others hitting this discussion may not be able to, so falling back to earlier ways is still valid. Even the OP could run into scenarios where CIM is not possible. – postanote Sep 20 '18 at 01:01
  • [There are](https://stackoverflow.com/questions/43735767/delete-user-profile-using-powershell) multiple [questions in a quick](https://stackoverflow.com/questions/49549752/delete-user-profiles-with-powershell) search that do WMI deletions. – Maximilian Burszley Sep 20 '18 at 01:18
0

Take care when deleting profiles, you don't want to hit machine special accounts. The Win32_UserProfile class has a LastUseTime property you can rely on.

$session = New-CimSession -ComputerName $cn
$gcimParams = @{
    'CimSession' = $session
    'ClassName'  = 'Win32_UserProfile'
    'Filter'     = 'RefCount<1 and Special="false" and Loaded="false"'
}
$profileList = (Get-CimInstance @gcimParams).Where{$PSItem.LastUseTime -lt (Get-Date).AddMonths(-6)}

foreach ($user in $profileList)
{
    $user | Remove-CimInstance -CimSession $session
}
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63