Folder deletion of Users home directories

  • Thread starter Thread starter Nik
  • Start date Start date
N

Nik

Can anyone help us, we are a local college and every year we have to
take off a huge number of users form active directory. This is the
colleges first year on active directory and we are encoutering
problems with a script complied from MS technet... The script should
read a text file and delete that particular username's AD account and
their corresponding home directory. The script does delete the account
but the folder doesn't get deleted please help. the script is as
follows... any advice on how to fix this would be much appreciated.

Nik Patel

PS - please ignore the backup line and the create and copy folders
part this was incorporated so we could do a quick restore for students
if necessary. This is now on tape backup.

Dim FSO, TS, username, obj, home, objusr, backup

backup = "\\lsc-pcadmin3\data\"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set TS = FSO.OpenTextFile("users.txt")

on error resume next

do until TS.AtEndOfStream
username = TS.readline

'Get Home Directory
set objusr = GetObject("WinNT://lsc/"& username &", user")
home = objusr.HomeDirectory
'wscript.echo backup&username

'Create Destination Folder
FSO.CreateFolder(backup&username)

'Copy home directory
FSO.CopyFolder home , backup&username

'Delete home directory
FSO.DeleteFolder home , username
set objusr= nothing

'Delete User account
set obj = GetObject("WinNT://lsc")
obj.Delete "user" , username
set obj = nothing
TS.SkipLine

Loop
ts.close
WScript.Echo "Done"
 
I don't know VB, but you can run a simple CMD script from your desktop.

Open a CMD prompt and type
DelUserFromFile PathToFileName.ext

DelUserFromFile.bat would contain:

@echo off
if {%1}=={} @echo Syntax: DelUserFromFile FileName&goto :EOF
setlocal
set file=%1
set file="%file:"=%"
if not exist %file% @echo %file% not found.&endlocal&goto :EOF
for /f "Tokens=*" %%d in ('type %file%') do set u=%%d&call :deluser
endlocal
goto :EOF
:deluser
set found=N
set fullname=???
set Home=???
for /f "Tokens=*" %%u in ('net user "%u%" /domain') do set line=%%u&call :parse
if "%found%" EQU "N" @echo %u% not found
net user "%u%" /delete /domain
if "%Home%" EQU "???" goto report
RD /s /q "%Home%"
:report
@echo %u% - %fullname% - %Home% - Deleted.
goto :EOF
:parse
if "%found%" EQU "Y" goto parse2
if /i not "%line:~0,9%" EQU "User name" goto :EOF
set wrk1="%line:~29,99%"
:parse1
set wrk2=%wrk1: "="%
if /i not %wrk1% EQU %wrk2% goto parse1
if /i not %u% EQU %wrk2% goto :EOF
set found=Y
:parse2
if /i "%line:~0,9%" EQU "Full Name" set fullname=%line:~29,99%
if /i "%line:~0,14%" EQU "Home directory" set Home=%line:~29,99%


NOTE: I didn't test this, so you may have to tweak it.



Can anyone help us, we are a local college and every year we have to
take off a huge number of users form active directory. This is the
colleges first year on active directory and we are encoutering
problems with a script complied from MS technet... The script should
read a text file and delete that particular username's AD account and
their corresponding home directory. The script does delete the account
but the folder doesn't get deleted please help. the script is as
follows... any advice on how to fix this would be much appreciated.

Nik Patel

PS - please ignore the backup line and the create and copy folders
part this was incorporated so we could do a quick restore for students
if necessary. This is now on tape backup.

Dim FSO, TS, username, obj, home, objusr, backup

backup = "\\lsc-pcadmin3\data\"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set TS = FSO.OpenTextFile("users.txt")

on error resume next

do until TS.AtEndOfStream
username = TS.readline

'Get Home Directory
set objusr = GetObject("WinNT://lsc/"& username &", user")
home = objusr.HomeDirectory
'wscript.echo backup&username

'Create Destination Folder
FSO.CreateFolder(backup&username)

'Copy home directory
FSO.CopyFolder home , backup&username

'Delete home directory
FSO.DeleteFolder home , username
set objusr= nothing

'Delete User account
set obj = GetObject("WinNT://lsc")
obj.Delete "user" , username
set obj = nothing
TS.SkipLine

Loop
ts.close
WScript.Echo "Done"


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Back
Top