Avatar of James Atherton
James Atherton
Flag for United States of America asked on

clearing multiple similar folders

I wrote a script that removes unused user folders in powershell by comparing the homedirectory in AD, comparing it to the "users" directory and removing the folder using a combo of robocopy and remove-item.  I would like to do the same to my profiles directory, however, we have had several versions of terminal server over the years so let's say we have c:\profiles\joeuser, we also have c:\profiles\joeuser.v2 and c:\profiles\joeuser.v6  I would like to clear all of them if they exist, and not everyone has a v2, or a v6.  Hope I am making sense.  Is there a way without hundreds of lines of code to write blank to each one, and then remove each one based solely on the joeuser part of the folder?  Below is my remove folder function, I am hoping that with a little tweeking I can have this same function handle a single instance like the user folder or a multiple instance like the profiles folder.


function Remove-UserFolderCompletely
   {
       Param(
        [string]$directory
    )

      $confirm = Read-Host "You are about to remove" $directory "are you sure? [Y] Yes [N] No"

      if ($confirm.ToLower() -eq "y")
      {
         # create a temporary (empty) directory
         Write-Host "Preparing for removal of " $directory
         $parent = $usersFolder #[System.IO.Path]::GetTempPath()
         [string] $name = [System.Guid]::NewGuid()
         $tempDirectory = New-Item -ItemType Directory -Path (Join-Path $parent $name)
            $removalDirectory = (Join-Path $parent $directory)
            Write-Host "Temp Directory Created:" $tempDirectory
         Write-Host "Running removal of" $directory
      
         robocopy $tempDirectory $removaldirectory /e /MIR | out-null
         Remove-Item $removaldirectory -Force | out-null
         Remove-Item $tempDirectory -Force | out-null
         Write-Host "Completed removal of" $directory
      }
   }

Powershell

Avatar of undefined
Last Comment
Hayes Jupe

8/22/2022 - Mon
Hayes Jupe

If your simply looking to clean out old user profiles, delprof2 remains the tool of choice for most admins.
If no - can you explain what your trying to achieve a little more - i read and re-read your first paragraph - but i'm not sure what the actual goal is here? what's the scenario or issue your trying to fix ? (assuming its not simply removing old profiles in their entirety)
James Atherton

ASKER
Hayes, I often find out about offboards long after I should, so I end up having to do a whole bunch at one time, so I was simply writing a script that looks at the terminated OU in AD, compares the username from homedirectory (because of some name changes and such) to the users folder on the server as well as the local users folder on the 5 terminal servers we have and delete's the ones that match the above list. it saves quite a lot of time.

I would like to do the same to the profiles directory on the file server, but as stated because of the few different versions of terminal server we have had there can be up to 3 folders for each user.  I would like to add logic to the script to clear them as well.  I had not considered using delprof2 and found a PS script that runs that based on an AD query, so I will take a look and see about doing the same.
Hayes Jupe

ok, that makes sense... so you want to offboard - but take a copy of user data prior to deleting.

given that you only get a .v2 etc when multiple OS versions are in use - you may have a non .v2 (etc) that is still the latest, so in order to back up only the latest, you would need to compare ntuser.dat dates... or just copy multiple, then use delprof2 to remove.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
James Atherton

ASKER
Also, with delprof2 I have the following config.  DC - {Three DFRS replicated file servers containing a copy of user profiles} - {Five Terminal Servers}  what do I actually run the delprof against to delete the profile information?  The DC? The Terminal Servers? or the File Servers?  All of the above?
ASKER CERTIFIED SOLUTION
Hayes Jupe

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.