Link to home
Start Free TrialLog in
Avatar of msidnam
msidnamFlag for United States of America

asked on

Script that will run a command and delete specific folders

I need to find a script that will delete folders in the C:\users folder, but keep some. I also need to run two commands on a set of folders and then delete them also in the c:\users folder.

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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msidnam

ASKER

Let me back track a little. We use a program called FSLogix that uses a file share to mount a vhd file for the profiles. It will create a C:\users\<username> folder as well as a c:\users\local_<username> folder for each user. When the users log off, the software is supposed to delete both of these folders. However, it doesnt always do this. Talking with support they say its a permissions issue, but I've looked at the permissions and everything looks correct. And it does delete them for the most part, but for some users it doesn't delete them. When this happens, the next time they log in, the software sees the C:\users\<username> folder (that should have been removed during the last log off) and says "oh look, you already have local profile and I dont need to mount any profile from the file share." Because of this, none of their desktop settings or saved files appear.

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.

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.

@msidnam
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
)

Open in new window

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.
Avatar of msidnam

ASKER

Thank you. I'm going to try the delprof2 and the code and see which one works the best. I'll report back.
Avatar of msidnam

ASKER

for the delprof2 is their any way to tell it to delete the profile no matter what the age is?

I haven't verified but try the switch:


/d:0


Or, just leave it out

Avatar of msidnam

ASKER

if i do /d:0 it doesnt like it. it wants a number other than 0 and default is 1.

In that case, use oBdA's powershell method.


Have you tried murugesandins' .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
)

Open in new window

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

Open in new window


Avatar of msidnam

ASKER

For the .bat method i would need it to be silent and not ask to press enter. I want to be able to run the script at night around 4 or 5am without and user interaction.

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
  )
)


Open in new window

Avatar of msidnam

ASKER

I gave one of my IT guys oBdA's script and from that script he created one for me that was tailored to our RDP servers.

I will be keeping the delprof2 in my tool belt as well.

thank you.