Delete files based on date selection Powershell script

This is more an Aide-mémoire than a help document, I do not profess to be brilliant at scripting or Powershell, but this works.

It is not time sensitive in the time it takes to run, only about 10 seconds so it is OK for me 🙂

If you get anything from it, good luck to you

# This is used to delete files from a selected folder
# If there are no files with yesterdays date, drop out

# Folder to clean the files
$Clean_Up_Dir = "XXXXXXXXXXXXXXX"
cd $Clean_Up_Dir
$date = Get-Date
#Just get the date
$Yesterday = $date.Date.AddDays(-1)
# Look for files with the .bac extension
$file = dir | Where-Object {$_ -like "*.bac"}
$file_start_date = 0

foreach ($items in $file)
{
#See if there are any file created yesterday, if the are it means that we can delete all of the .bac file
#except yesterdays, if this is null there is no backup and therefore we can not delete anything

 if ($items.LastWriteTime.Date.Date -eq $Yesterday)
 {
 $file_start_date = $items.LastWriteTime.Date.Date
 break
 }
}

if ($file_start_date -eq 0) {}
else
{
# now delete all files that are less that the start date
$file_to_Delete = dir | Where-Object {$_ -like "*.bac" -and $_.LastWriteTime.Date.date -lt $file_start_date}
foreach ($items_delete in $file_to_delete)
 {
 Remove-Item $items_delete.Name -Recurse -Force
 }
}

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