Clean up Terminal Server 2003 profiles


Terminal server profiles can become a bit bloated after a while. I wrote this a few years ago to clean them up on the file server in the environment.

It is written in powershell!!!

# Folder to clean
$Profile_Dir = "F:\UserTsProfile"
# Folder where the log files will be kept
$LogFolder = "F:\TSCleanupLogs" $date = Get-Date
$Day = $date.Day
$Month = $date.Month
$Year = $date.Year
$DateStr = ($Day.ToString() + "-" + $Month.ToString() + "-" + $Year.ToString())
#Check if the folder for the error logs is present
#if not created it if (!(Test-Path $LogFolder))
{
#echo "No Folder"
mkdir $LogFolder
}
$LogFileName = "Error_Log_"+$DateStr+".log"
$LogFilePath = $LogFolder+"\"+$LogFileName cd $Profile_Dir
$file = dir | Where-Object {$_.psIsContainer -eq 'Directory'} foreach ($items in $file)
{
switch ($items.Name)
{ #Enter the name of directories that you do not want to clean
"Administrator" {echo $items.Name" Not to be searched"}
"Copy" {echo $items.Name" Not to be searched"} default { #This is used to trap general errors the script
#will continue and ignore the error trap
{
$error = $_.Exception.GetType()
#Write-Host "Caught $error inside function"
$LogFileCheck = Test-Path $LogFilePath if ($LogFilePath -eq "False")
{
New-Item $LogFilePath -type File
} Add-Content $LogFilePath ("There was an error run the job -"+$Error)
continue
} cd $items.FullName
cd "Application Data" $Sub_Dir = dir | Where-Object {$_.psIsContainer -eq 'Directory'} foreach ($item in $Sub_Dir)
{
switch ($item.Name)
{
#Windows / system specific to keep
"Identities" {}
"Microsoft" {} #Client Specific to keep
"2XClient" {}
"IFS" {}
"OpenOffice.org2" {} default {#echo $item.Name
Remove-Item $item.Name -Recurse
} } } #Clean up what is left in the Microsoft folder
cd $items.FullName
cd "Application Data\Microsoft" $Sub_Dir = dir | Where-Object {$_.psIsContainer -eq 'Directory'} foreach ($item in $Sub_Dir)
{
switch ($item.Name)
{ #Windows specific to keep
"Credentials" {}
"CryptnetUrlCache" {}
"Internet Explorer" {}
"SystemCertificates" {} #Client Specific to keep
default {
#debug line
#echo $item.Name
Remove-Item $item.Name -Recurse -Force
} } } #Remove the contents of the cookies folder
#System specific
cd $items.FullName
cd "cookies" Get-ChildItem | Remove-Item -Recurse #Remove the contents of the recent folder
#System specific
cd $items.FullName
cd "recent"
Get-ChildItem | Remove-Item -Recurse #Client Specific
#Deletes UserData Folder
cd $items.FullName
$searchpath =
if ((Test-Path "UserData"))
{
Remove-Item "UserData" -Force
}
} }
} Exit
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s