asked on
Script that will run a command and delete specific folders
for example I would need to get a listing of the folders and then delete c:\users\bob and c:\users\alice but keep c:\users\jane and c:\john.
Then I would need to run:
takeown /f c:\users\local_tiffany /r /d y
icacls c:\users\local_tiffany /grant kr\administrator:F /T
and then delete c:\users\local_tiffany
The folders I need to keep will always be the same (about 10) but the folders to delete and run the command on will always be different.
Have you looked at DelProf2?
https://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool/
In your case, to keep or delete profiles matching certain names, use the /ed or /id switches.
We followed the same at other OS excluding Windows (using mount)
Sample batch file at windows to get available USERNAME 's
@ECHO OFF
FOR /F "delims=" %%D IN ('DIR /B /O:D C:\Users ^| C:\Windows\System32\findstr.exe /v "Administrator Public murugesandins john jane"') DO (
ECHO C:\Windows\System32\takeown.exe /f C:\Users\%%D /r /d y
ECHO C:\Windows\System32\icacls.exe C:\Users\%%D /grant kr\Administrator:F /T
ECHO Press Ctrl C to stop executing this batch file or
SET /P ENTER="Press Enter to continue: "
REM Remove REM at following lines:
REM C:\Windows\System32\takeown.exe /f C:\Users\%%D /r /d y
REM C:\Windows\System32\icacls.exe C:\Users\%%D /grant kr\Administrator:F /T
REM Press Ctrl C to stop executing this batch file or
)
Before deleting or writing that using batch file, better make a backup(.zip/.gz/...) for those users.After many years our team may require base version 0.0 which was written many years ago.
ASKER
ASKER
I haven't verified but try the switch:
/d:0
Or, just leave it out
ASKER
In that case, use oBdA's powershell method.
Have you tried
' .bat method yet?Here's a similar .bat file method I use successfully. Both oBdA's and this method use WMI to delete the profile.
You can edit 'alice' and 'bob' to your needs.
It creates an result log c:\wmic_delprof.err for post examination.
@echo off
for %%a in (alice bob) do (
wmic /node:localhost path win32_UserProfile where LocalPath="c:\\users\\%%a" Delete 2>>c:\wmic_delprof.err
wmic /node:localhost path win32_UserProfile where LocalPath="c:\\users\\local_%%a" Delete 2>>c:\wmic_delprof.err
)
Basically, it deletes both profiles for each user, in this case, alice and bob. e.g. c:\users\alice and c:\users\local_alice:
wmic /node:localhost path win32_UserProfile where LocalPath="c:\\users\\alice" Delete
wmic /node:localhost path win32_UserProfile where LocalPath="c:\\users\\local_alice" Delete
wmic /node:localhost path win32_UserProfile where LocalPath="c:\\users\\bob" Delete
wmic /node:localhost path win32_UserProfile where LocalPath="c:\\users\\local_bob" Delete
ASKER
It would also need to be able to dynamically list the folders since they could be different each time. For example one server could have c:\users\bob and c:\users\local_bob one day and not the other.
> ... need it to be silent
Use and set the Task Scheduler to desired run time of bat file
> ... need to be able to dynamically list the folders since they could be different each time. For example one server could have c:\users\bob and c:\users\local_bob one day and not the other.
I presume there are some profiles that must not be deleted, which can be excluded per this code. Just revise the SET EXCLUDE line to your needs, adding all users to keep:
@echo off
setlocal enabledelayedexpansion
set exclude=excluser1 excluser2
for /d %%a in (c:\users\*) do (
echo %%a | findstr /i "%exclude%"
if "!errorlevel!" equ "1" (
wmic /node:localhost path win32_UserProfile where LocalPath="c:\\users\\%%a" Delete 2>>c:\wmic_delprof.err
wmic /node:localhost path win32_UserProfile where LocalPath="c:\\users\\local_%%a" Delete 2>>c:\wmic_delprof.err
)
)
ASKER
I will be keeping the delprof2 in my tool belt as well.
thank you.
ASKER
When the user logs off their profile is no longer listed in the user profiles in System. So thats why I just need a script that will delete those folders.