need a script to find old user folders

i am trying to write a script that will find folders on a file server that have no AD account attached to them.  

for example we have user1 map their home directory which is stored on another file server so if we delete that user the directory is on another server

but all usernames match the directories

so user1 has the directory user1
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amoosAuthor Commented:
thank you.  will this delete folders that have no user account?

user one maps there home directory from another server, i.e. AD is on one server and the home directories are on another.

have a person who deleted a lot of accounts but did not delete thier home directories to so what i am looking for is a script or program that will find the directories that dont have AD accounts associated with them

tons of thanks in advance
Bill PrewIT / Software Engineering ConsultantCommented:
Why not just export from AD a list of all current valid user accounts to a TXT file, and then use a small VBS (or DOS or PS1) script to look at all first level subfolders to the user folder base directory, and delete any that have a name that isn't in the TXT file?

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

amoosAuthor Commented:
and what script could i use?  i am new to scripting.
Bill PrewIT / Software Engineering ConsultantCommented:
If we go with the assumption of a list of active accounts, I will build a small sample script and post it.

Bill PrewIT / Software Engineering ConsultantCommented:
Here is a very small and simple BAT script approach that should do the job.  Adjust the paths near the top to reference the base folder where the user account folders exist, and then file name of the current users list file you produce.

Right now the script is in "test mode" - I have added an ECHO command before the RD (remove directory) command.  This means when you run the script as is, it won't actually remove any folders, just display to the screen the RD command that would have been done, showing the folder that would have been removed.  This allows you to check and make certain it picks the right folders to remove.

Remember, this assumes ALL folders in the base directory are user folders, and so any folder that has a name that isn't in the user list file will be removed.

Run from within a DOS window so you can see any error messages that are displayed, as well as the RD commands that would be executed.  When you want to actually remove the folders, take the "ECHO " off of the RD line.  Also make sure you run with privileges to be able to delete all these folders and their content.

@echo off

rem Define base users forlder where each account has a subfolder
set BaseDir=B:\EE\EE28823459\Users

rem Define location of list file of all current users in AD (these folders will not be deleted)
set UserList=users.txt

rem Make sure base directory exists
if not exist "%BaseDir%\%" (
  echo *ERROR* base directory does not exist [%BaseDir%]
  exit /b

rem Make sure user list file exists
if not exist "%UserList%" (
  echo *ERROR* active users list file does not exist [%UserList%]
  exit /b

rem Get all folders in the base folder, and then see if they are in the users list file,
rem if they are not in the active users list then remove their whole directory
for /f "tokens=1" %%U in ('dir /b /ad "%BaseDir%" ^| findstr /b /e /l /i /v /g:"%UserList%"') do (
  ECHO rd /s /q "%BaseDir%\%%U"

Open in new window

David Johnson, CD, MVPOwnerCommented:
powershell hack
$profiles = get-childitem \\dc2\User$
foreach ($profile in $profiles){
        $username = Get-ADUser -Identity $profile.Name -Properties * -ErrorAction SilentlyContinue
        # remove -whatif if this works in your environment
        Remove-Item $profile.FullName -Recurse -WhatIf

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.