PowerShell Script To Delete Only Account Unknown User Profiles

  Рет қаралды 7,329

URTechDotCa

URTechDotCa

Күн бұрын

Пікірлер: 17
@emanuelcortes9339
@emanuelcortes9339 7 ай бұрын
I would add a check to make sure that the device can actually talk to the domain controller before performing any of this. Ran into that during testing where a user took a shared laptop home to finish what they were working on. Woulda sucked if I had deleted every profile as a result. Otherwise this script is awesome and I really appreciate you all doing the homework for us on this.
@gamingfromjohnwayne
@gamingfromjohnwayne 3 ай бұрын
I did this on user property's and now won't let me close out says error applying security? Don't know how fix
@shreyas2595
@shreyas2595 11 ай бұрын
Thanks a lot Bro. Good Job!
@imanthierandi4751
@imanthierandi4751 6 ай бұрын
can we do this for disable user accounts? or a using last logon dates?
@brematthews384
@brematthews384 Жыл бұрын
Very helpful!
@albertburger7912
@albertburger7912 10 ай бұрын
What's the difference btw directly deleted it from the menu than using ps. it's more easy to delete them on the menu than using instructions?
@s0cks1985
@s0cks1985 8 ай бұрын
Because on corporate servers you may have hundreds of unknown accounts.
@fabrythrash6648
@fabrythrash6648 Жыл бұрын
Fantastic !
@Sourkeys
@Sourkeys 11 ай бұрын
I'm getting: InvalidOperation [Remove-wmiobjet] COMException Remote-wmiObject -inputObject $profile Any Ideas?
@timostraetemans5095
@timostraetemans5095 11 ай бұрын
powershell not running as admin?
@timostraetemans5095
@timostraetemans5095 7 ай бұрын
if you're running it on a client it may still be loaded as W10/W11 preload data. Check if that's the case: Get-WMIObject win32_userprofile -computername localhost -Filter "Loaded='True'" | Select SID,LocalPath,Loaded
@chadkupar6369
@chadkupar6369 7 ай бұрын
It's not "Remote" it's Remove
@philipkatana
@philipkatana 10 ай бұрын
Thank you very much
@timostraetemans5095
@timostraetemans5095 11 ай бұрын
i love your script! Maybe add logging as it may be handy. As wel as the path of the folder that is deleted so its added to the log. Then deploy it in a GPO and another issue is automated :) 15-6-2024, change log: - changed logging to 'local folder\logs' with cleaning - added colors for easier output reading - tested on 2012R2/2016/2019/2022, added check as it doesn't work on 2012R2 and older, for clients that means windows 8 and older! -- tested with GPO/Task scheduler, runs fine Script, copy from clear to below and save it in DeleteOldProfiles.ps1: Clear # Defining and Setting up logging $scriptpath = Split-Path $MyInvocation.MyCommand.path $logfile = @(mkdir ($scriptpath + '\Logs') -Force).fullname + "\$($scriptfilename)-$($env:computername).$($env:userdnsdomain)" + "-Log-$(Get-Date -f "yyyyMMdd-HHmmss").log" Start-Transcript -Path $logfile #Clean Logs $Folder = ($scriptpath + '\Logs') #Delete files older than 6 months Write-Output "Clean log Folder" Get-ChildItem $Folder -Recurse -Force -ea 0 | ? {!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-180)} | ForEach-Object { $_ | del -Force Write-Output "Deleting $Folder\$_.FullName" } Write-Output "Logs purged" #Detect OS Version as it only works correctly in Server 2016 and newer $OSVersion = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName if ($OSVersion -notmatch "2016|2019|2022") {Write-host -ForegroundColor Red "Windows server 2016, 2019, 2022 not detected! This script doesn't run correct on older versions, it deletes ALL profiles! Stopped Script!" Stop-Transcript Break } Write-Host -ForegroundColor Cyan "Windows 2016 or newer required, $OSVersion"detected. #Get Profiles $userProfiles = Get-WmiObject -Class Win32_UserProfile foreach ($profile in $userProfiles) { $userSID = $profile.SID $userAccount = $null $LocalPath = $profile.LocalPath #Try to get the user account associated with the profile try { $userAccount = [System.Security.Principal.SecurityIdentifier]::new($userSID).Translate([System.Security.Principal.NTAccount]).Value } catch { #An exception occurs when the user account doesn't exist } #Check if a user account was found if ($userAccount -eq $null) { #Delete the user profile Write-Host -ForegroundColor Green "Deleting user profile $LocalPath with SID $userSID" Remove-WmiObject -InputObject $profile } } Write-Host -ForegroundColor Cyan "Finished cleaning User profiles." Stop-Transcript
@gezeo750
@gezeo750 7 ай бұрын
How would you keep this from just removing service accounts, administrator or defaultuser accounts? Or does it do it already?
@timostraetemans5095
@timostraetemans5095 7 ай бұрын
@gezeo750 it only deletes accounts that cannot be resolved into its own name, so if you delete the administrator account the profile becomes orphaned, and the script will delete it. you can check the profile, on the security tab, if you see a SAM number instead of its SamAccountName, the account is deleted. DefaultUser isn't an account you can login with. Put a # before 'Remove-WmiObject -InputObject $profile' to test it without actually deleting any profiles.
What Can Be Safely Deleted From C:\Windows?
7:34
URTechDotCa
Рет қаралды 7 М.
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Get Paid $8 🤑 Every Min. (AUTOPILOT) - Make Money Online 2025
5:06
PowerShell Script to Delete Multiple Users' Temporary Files
8:37
The PowerShell Playground
Рет қаралды 156
Best Virus Removal Tools: Cleaning a deeply infected system
8:31
PC Security Channel
Рет қаралды 940 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 243 М.
How to create a juniper IOS image using Balena Etcher
8:07
DEEPSEEK Vs CHATGPT There Is A  Clear Winner !!
15:53
Rick Aqua
Рет қаралды 17 М.
21 One Line PowerShell Networking Commands
12:41
PowerShell Engineer
Рет қаралды 4,9 М.
How To Delete A User Account On Windows 11
3:52
ProgrammingKnowledge2
Рет қаралды 323 М.
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН