From c3832e8c60c6377cec380d13223cf003b8b94d3f Mon Sep 17 00:00:00 2001 From: poslop Date: Fri, 20 Sep 2024 12:33:47 -0500 Subject: [PATCH] Organized --- scripts/DeleteOldProfilesDirectory.ps1 | 23 +++++++++++++++++++ ...er.ps1 => DeleteOldProfilesNetProfile.ps1} | 4 +++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 scripts/DeleteOldProfilesDirectory.ps1 rename scripts/{NetUser.ps1 => DeleteOldProfilesNetProfile.ps1} (92%) diff --git a/scripts/DeleteOldProfilesDirectory.ps1 b/scripts/DeleteOldProfilesDirectory.ps1 new file mode 100644 index 0000000..1dc296f --- /dev/null +++ b/scripts/DeleteOldProfilesDirectory.ps1 @@ -0,0 +1,23 @@ +# Delete old profiles based on the home users directory last modification date + +$daysInactive = 14 # Specify the number of days of inactivity +$inactiveDate = (Get-Date).AddDays(-$daysInactive) +$homeDirectoryPath = "C:\Users" # Path to user home directories + +# Get all user profiles +Get-WmiObject Win32_UserProfile | ForEach-Object { + $profile = $_ + $userFolder = $profile.LocalPath + + # Check if the profile folder exists and get the last modification date + if (Test-Path $userFolder) { + $lastWriteTime = (Get-Item $userFolder).LastWriteTime + + if ($lastWriteTime -lt $inactiveDate) { + Write-Host "Deleting user profile: $($profile.LocalPath) Last Modified: $lastWriteTime" -ForegroundColor Red + + # Remove the user profile + $profile.Delete() + } + } +} diff --git a/scripts/NetUser.ps1 b/scripts/DeleteOldProfilesNetProfile.ps1 similarity index 92% rename from scripts/NetUser.ps1 rename to scripts/DeleteOldProfilesNetProfile.ps1 index 2e4e18a..bebd7e8 100644 --- a/scripts/NetUser.ps1 +++ b/scripts/DeleteOldProfilesNetProfile.ps1 @@ -1,3 +1,5 @@ +# Delete old profiles based off of the last login in AD + $daysInactive = 14 # Specify the number of days of inactivity $inactiveDate = (Get-Date).AddDays(-$daysInactive) $homeDirectoryPath = "C:\Users" # Path to user home directories @@ -22,4 +24,4 @@ Get-WmiObject Win32_UserProfile | ForEach-Object { $profile.Delete() } } -} \ No newline at end of file +}