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

Read directory and move files

Powershell is really cool and you can do a pile of stuff with it, I had dabbled with this a few years ago for some other bits.

Because of this I am going to create a category and as I write more will be posting them 🙂

We needed to move some files from one place to another and so after a few minutes / hour 🙂 I cam up with this.

# Folder to read to move files
$vDir = “F:\PackingSpecApproval\Temp_To_File\”
#$vDir = “X:\PackingSpecApproval\Temp_To_File\”

#Folders to move the specs to
$vMoveToDir = “F:\PackingSpecApproval\Approved\China\”
#$vMoveToDir = “X:\PackingSpecApproval\Approved\China\”
# The folder to read specs that need filing that would have been translated
$vtransdir = “F:\PackingSpecApproval\To_Be_Translated\”
#$vtransdir = “X:\PackingSpecApproval\To_Be_Translated\”
# This files the specs
$file = Get-ChildItem $vDir -Name
foreach ($i in $file)
{

if ($file -eq $null) {} else
{
$filechar = $i.Substring(0,1)
$filename = $i.PSChildName
$movetodirectory = $vMoveToDir+$filechar
$movefrom = $vDir+$filename
$moveto = $vMoveToDir+$filechar
MOVE $movefrom $moveto
}
}
# This files the specs that would need translating
$transfile = Get-ChildItem $vtransdir -Name
foreach ($i in $transfile)
{

if ($transfile -eq $null) {} else
{
$filechar = $i.Substring(0,1)
$filename = $i.PSChildName
$movetodirectory = $vMoveToDir+$filechar
$movefrom = $vtransdir+$filename
$moveto = $vMoveToDir+$filechar
MOVE $movefrom $moveto
}

}

# clean up the info files

cd “F:\PackingSpecApproval\Info\AmmendedAwaitingTranslation\”
Get-ChildItem | Remove-Item -Recurse
cd “F:\PackingSpecApproval\Info\AwaitingTranslation\”
Get-ChildItem | Remove-Item -Recurse
cd “F:\PackingSpecApproval\Info\WatcherFolder\”
Get-ChildItem | Remove-Item -Recurse