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