none
Script To delete profiles except RRS feed

  • Question

  • Hi Guys, I need to delete user profiles older then 30 days, however, I need to exclude these profiles

    <g class="gr_ gr_93 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="93" id="93">tempadmin</g>,<g class="gr_ gr_460 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="460" id="460">localadmin</g>,defaultuser0,administrator,test1

    I found a few <g class="gr_ gr_242 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="242" id="242">script</g> on here but have no idea how to edit it to make it run with my exclusions, Also what is the best way to get these to run? schedule task via <g class="gr_ gr_291 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="291" id="291">gpo</g>? and will this do in background when any user ''logs on'' what is the best way to make sure it works as should

    Example of script found (<g class="gr_ gr_514 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" data-gr-id="514" id="514">thankyou</g>) :)

    Function Cleanup-UserProfiles
    <#
    .Parameter ComputerName
    The computer on which to clean profiles.

    .Parameter AgeLimit
    All profiles older than this number of days will be removed

    .Parameter Exclude
    Comma separt
    #>
    {
      [CmdletBinding(
         SupportsShouldProcess=$true,
        ConfirmImpact="High"
      )]param ($computerName= '.',$AgeLimit='60', $Exclude)

      $dateLimit = (get-date).adddays(-1 * $agelimit)
      $userprofiles = Get-WmiObject -Class Win32_UserProfile -ComputerName $computerName

      #the default exclusion list will prevent deletion of Administrator acount, Default accounts, System and Network Service
      $exclusionlist = @('S-1-5-19','S-1-5-18','S-1-5-20','-500$') + $Exclude | where-object {$_}

      foreach ($profile in $userprofiles) {
        #Check if profile is in date range
        $dateLastUsed = [datetime]::ParseExact(($profile.lastusetime -replace '\..+$',''),'yyyyMMddHHmmss',$null )
        if ( $dateLastused -ge $dateLimit){
            write-verbose "Skipping $($profile.sid) because it was last used $dateLastUsed"
            continue;    
        }

        #Check if profile matches an exclusion
        $MatchesExclusion = $false
        foreach ($comparison in $exclusionlist){
            if ($profile.sid -match $comparison -or $profile.localpath -match $comparison)
            {
                $MatchesExclusion = $true
                write-verbose "Skipping $($profile.sid) because it matches exclusion '$comparison'"
                break;
            }
        }
        if ($MatchesExclusion) {continue;}
        
        #Use ShouldProcess to prevent accidental removal of profiles
        $activity = "Remove profile for user $($profile.SID) from computer $Computername with local path $($profile.localpath)"
        if ($pscmdlet.ShouldProcess($activity)) {
            Write-Verbose "Attempting to $activity"
            $profile.Delete()       
        }
      }
    }

    • Moved by Bill_Stewart Friday, March 15, 2019 6:25 PM Unanswerable drive-by question
    Friday, January 25, 2019 9:25 AM

All replies