From 2f54d51b59985738143af78edb6e953b93af27db Mon Sep 17 00:00:00 2001 From: poslop Date: Mon, 10 Feb 2025 09:55:59 -0600 Subject: [PATCH] added exclusion logic --- scripts/DeleteOldProfilesNetProfile.ps1 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/DeleteOldProfilesNetProfile.ps1 b/scripts/DeleteOldProfilesNetProfile.ps1 index 8b01d74..6500795 100644 --- a/scripts/DeleteOldProfilesNetProfile.ps1 +++ b/scripts/DeleteOldProfilesNetProfile.ps1 @@ -3,15 +3,22 @@ $daysInactive = 14 # Specify the number of days of inactivity $homeDirectoryPath = "C:\Users" # Path to user home directories $domainName = "local" # Domain name that the pc is on +$excludedProfiles = @("Default User", "administrator") $inactiveDate = (Get-Date).AddDays(-$daysInactive) # Get all user profiles -Get-WmiObject Win32_UserProfile | ForEach-Object { +Get-WmiObject Win32_UserProfile | :Loop ForEach-Object { $profile = $_ $userFolder = $profile.LocalPath $userName = $profile.LocalPath.Split('\')[-1] # Get the username from the folder path + $nameIsExcluded = false + + foreach ($name in $excludedProfiles) { + if ($item -eq $userName) { + continue Loop + } # Get the last logon time for the local user $userLogon = Get-WmiObject -Class Win32_NetworkLoginProfile -Filter "Name='$domainName\\$userName'" | Select-Object -Property LastLogon