#requires -version 2 Function Delete-InactiveProfiles { <# .SYNOPSIS FileName: Delete-InactiveProfiles.ps1 Author: Alan dot Kaplan at VA dot GOV Date: 10-4-2013 Written to replace DelProf, this script deletes inactive user profiles from a local or remote computer. It supports arguments by position, and has a test parameter If you run it locally, you may supply host name, localhost or "." The logfile is tab delimited, you may use XLS extension to open in Excel. If you run the script interactively the log opens when the action is complete. .Parameter Computername The name of the computer .Parameter InactiveDays The number of days which have been passed before considering a profile inactive. The logfile is tab delimited, you may use XLS extension to open in Excel. .Parameter LogFile The full path to the logfile. .Parameter OverwriteLog If present, the log will be overwritten. .Parameter WhatIf If present, the script will run in test mode. No changes are made. .EXAMPLE Delete-InactiveProfiles.ps1 Server1 90 c:\mylogs\logfile.txt Delete profiles from Server1 older than 90 days and log results to c:\mylogs\logfile.txt .EXAMPLE Delete-InactiveProfiles.ps1 -ComputerName Server1 -InactiveDays 30 -logfile "c:\my logs\logfile.txt" -WhatIf Preview results of running a deletion of profiles from Server1 older than 30 days and log results to "c:\mylogs\logfile.txt" .LINK www.akaplan.com/blog/2013/10/delete-inactive-user-profiles-with-powershell .DESCRIPTION PowerShell Function to delete old profiles #> param( [Parameter(Mandatory=$True,Position=1)] [string] $ComputerName, [Parameter(Mandatory=$True,Position=2)] [int]$InactiveDays, [Parameter(Mandatory=$False,Position=3)] [string] $LogFile, [Parameter(Mandatory=$False,Position=4)] [switch] $OverwriteLog, [Parameter(Mandatory=$False,Position=5)] [switch] $WhatIf ) Function EchoAndLog { param([string] $strText) #Echo to host and append to file Tee-Object -Append -FilePath $logfile -inputobject $strText } ###################### Script begins ###################### $ErrorActionPreference = "Stop" #Define default log $lfd = $env:userprofile + "\desktop\$ComputerName" + "_UserProfileDeleteLog.txt" #prompt for logfile if (!$LogFile){ $logfile = Read-Host "Enter Path to tab delimited Logfile, or ENTER for default,`n$lfd" } #use default log if not specified if ($LogFile.Length -eq 0) {$LogFile = $lfd} #Delete old log if OverwriteLog set if ($OverwriteLog) {Remove-Item $LogFile -Force} #If log is new, add header if ((Test-Path $logfile) -eq $False) { $header = "Date`tPath`tDaysSinceUsed" Out-File -FilePath $logfile -inputobject $header } #special handling of alternate names for the local system if (($ComputerName.ToLower() -eq "localhost") -or ($ComputerName -eq ".")){ $ComputerName = $env:COMPUTERNAME } #WMI query excludes Administrator account and local service accounts Get-WmiObject Win32_UserProfile -filter "not SID like '%-500' and Special = False" -ComputerName $ComputerName | ForEach-Object{ $ProfilePath = "\\$computername\"+$_.LocalPath.Replace(":","$") if ((test-Path $ProfilePath)-eq $false) { remove-wmiobject -inputobject $_ -WhatIf:$WhatIf EchoAndLog "$(Get-Date)`t$ProfilePath`t`tNot Found" }ELSE{ #I think getting timestamp from NTUser.DAT is more reliable than WMI LastUseTime $LastUsedDays = ((get-date)-(Get-Item -force "$profilePath\ntuser.dat").LastWriteTime).days $LastUsedDays = [math]::abs($LastUsedDays) if($LastUsedDays -gt $InactiveDays){ #WMI to delete the profile remove-wmiobject -inputobject $_ -WhatIf:([bool]$WhatIf) $strTxt = "$(Get-date)`t$ProfilePath`t$LastUsedDays`t" if($WhatIf -eq $false){ $strTxt += "Deleted" }ELSE{ $strTxt += "Would be Deleted" } EchoAndLog $strTxt }Else{ write-host "Skipping $ProfilePath, used within $LastUsedDays days"} } } #Open logfile if done interactively if ($args.count -eq 0) { Invoke-Item $logfile } }