Removing user profiles in Windows
#Self elevate powershell to BuiltIn administrator account
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
#List of the profiles that we need to keep
$profilesToKeep='Public','Administrator','Default','NetworkService','LocalService','systemprofile'
#Add the profile objects to a variable titled profiles exclude profiles that we need to keep
$profiles = Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -notin $profilesToKeep }
#Count the number of profiles to remove
"Number of user profiles to remove = $($profiles.Count)"
#Create a foreach loop to iterate through the profiles variable/object, print removed and remove the profile
foreach ($profile in $profiles){Write-Output $profile | Select LocalPath; Write-Output "removed"; $profile | Remove-CimInstance}
#Pause for viewing
Read-Host -Prompt "Press enter to continue"
Method 2
#Self elevate powershell to BuiltIn administrator account
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
$logs = get-eventlog system -source Microsoft-Windows-Winlogon -After (Get-Date).AddDays(-200);
$res = @(); ForEach ($log in $logs) {if($log.instanceid -eq 7001) {$type = "Logon"} Else {Continue}
$res += New-Object PSObject -Property @{Time = $log.TimeWritten; "Event" = $type; User = (New-Object System.Security.Principal.SecurityIdentifier $Log.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])}};
#$res.user | Sort-Object | Get-Unique
$UsersToKeep = ($res.user | Sort-Object | Get-Unique).value
$splitUsers = $UsersToKeep | ForEach-Object {
$_ -split "\\" | Select-Object -Skip 1
}
#List of the profiles that we need to keep
$profilesToKeep = 'Public','Administrator','Default','NetworkService','LocalService','systemprofile'
$profilesToKeep += $splitUsers
#Add the profile objects to a variable titled profiles exclude profiles that we need to keep
$profiles = Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -notin $profilesToKeep }
#Count the number of profiles to remove
"Number of user profiles to remove = $($profiles.Count)"
#Create a foreach loop to iterate through the profiles variable/object, print removed and remove the profile
foreach ($profile in $profiles){Write-Output $profile | Select LocalPath; Write-Output "removed"; $profile | Remove-CimInstance}
#Pause for viewing
Read-Host -Prompt "Press enter to continue"
Removing Teams Cache
# Get a list of all user profiles
$userProfiles = Get-WmiObject -Class Win32_UserProfile | Where-Object { -not $_.Special }
foreach ($user in $userProfiles) {
# Construct the path to the cache folder
$cachePath = Join-Path $user.LocalPath 'AppData\Roaming\Microsoft\Teams\Service Worker\CacheStorage'
# Check if the folder exists and delete it
if (Test-Path $cachePath) {
Remove-Item -Path $cachePath -Recurse -Force
Write-Host "Deleted cache for user: $($user.LocalPath)"
} else {
Write-Host "Cache folder not found for user: $($user.LocalPath)"
}
}