From 59570caec4b0eadb21eac6a8b1c6a7c081b9aae9 Mon Sep 17 00:00:00 2001 From: poslop Date: Fri, 20 Sep 2024 12:21:05 -0500 Subject: [PATCH] a --- README.md | 0 scripts/NetUser.ps1 | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 README.md create mode 100644 scripts/NetUser.ps1 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/scripts/NetUser.ps1 b/scripts/NetUser.ps1 new file mode 100644 index 0000000..2e4e18a --- /dev/null +++ b/scripts/NetUser.ps1 @@ -0,0 +1,25 @@ +$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 + $userName = $profile.LocalPath.Split('\')[-1] # Get the username from the folder path + + # Get the last logon time for the local user + $userLogon = Get-WmiObject -Class Win32_NetworkLoginProfile -Filter "Name='RPBLUEJAYS\\$userName'" | Select-Object -Property LastLogon + + if ($userLogon) { + $lastLogonDate = [Management.ManagementDateTimeConverter]::ToDateTime($userLogon.LastLogon) + + # Check if the user's last local logon date is older than the specified inactive date + if ($lastLogonDate -lt $inactiveDate) { + Write-Host "Deleting user profile: $($profile.LocalPath) Last Logon: $lastLogonDate" -ForegroundColor Red + + # Remove the user profile + $profile.Delete() + } + } +} \ No newline at end of file