Clear temp folders on Computer start with PowerShell

16. April 2015

This script will clear the common temp folders on Windows 7 client computers.

$date = Get-Date -Format "dd.mm.yy-HH:mm"
$win_temp_folder = "C:\Windows\Temp"
$outfile = "C:\tmp\clear_tmp.log"
$temp_folders = Get-Item C:\Users\*\AppData\Local\Temp
$os = (Get-WMIObject Win32_OperatingSystem).name

if ($os -like "*Windows 7*")
    {
    # CLEAR APPDATA LOCAL TEMP FOLDER PER USER
    foreach ($temp_folder in $temp_folders)
        {
        $user_temp_files = Get-ChildItem -Path $temp_folder
        $user_temp_files_count = ($user_temp_files | measure).Count
            if ($user_temp_files_count -gt 0)
            {
            echo "#########################################################################" | Out-File $outfile -Append
            echo "$date" | Out-File $outfile -Append
            $user_temp_folder_fullname = $temp_folder.FullName
            echo "Clearing $user_temp_folder_fullname" | Out-File $outfile -Append
            Remove-Item $user_temp_folder_fullname\* -Recurse -Force   
            }
        }
        # CLEAR WINDOWS TEMP FOLDER
        echo "Clearing $win_temp_folder" | Out-File $outfile -Append
        Remove-Item $win_temp_folder\* -Recurse -Force
    }

In line 4 we make sure to grab the \AppData\Local\Temp folder for every user on the machine and with line 7 we apply this to Windows 7 client PCs only. The script will write it’s activity in the $outfile.

I recommend applying this as a GPO start up script to make sure that none of the files in those folders are in use while the script runs: Computer-Configuration\Policies\Windows-Settings\Scripts\

PowerShell Version: 4
Modules: none

Monitoring a PING connection with PowerShell

This script will let you silently monitor a PING connection to 3 hosts. We apply this via GPO as a login script and it will monitor the user’s connection to those hosts and write connection losses to the outfile shown in line 4.

If a connection loss to one of the main hosts occurs we added 2 subhosts to check and see if the connection to the local gateway and filesever is OK.

$ErrorActionPreference= 'silentlycontinue'
$date_header = Get-Date -Format ddMMyy

$outfile = "PATHTOYOURLOG_$($env:USERNAME)_$($env:COMPUTERNAME)_$($date_header).log"
$host1 = "YOURHOST"
$host2 = "YOURHOST"
$host3 = "YOURHOST"
$sub_host1 = "YOURSUBHOST"
$sub_host2 = "YOURSUBHOST"
Get-Date -Format "dd.MM.yy HH:mm:ss" | Out-File $outfile -Append
echo "Starting script..." | Out-File $outfile -Append

        do
        {
        
        # HOST1
        Test-Connection $host1 -Count 4 | out-null
        if ([int]$? -eq 0)
            {
            $date = Get-Date -Format "HH:mm:ss"
            echo "########################################################" | Out-File $outfile -Append
            echo "$date Lost connection to $host1" | Out-File $outfile -Append
            # SUB-HOST1
            Test-Connection $sub_host1 -Count 1 | Out-File $outfile -Append
            # SUB-HOST2
            Test-Connection $sub_host2 -Count 1 | Out-File $outfile -Append
            }

        # HOST2
        Test-Connection $host2 -Count 4 | out-null
        if ([int]$? -eq 0)
            {
            $date = Get-Date -Format "HH:mm:ss"
            echo "########################################################" | Out-File $outfile -Append
            echo "$date Lost connection to Host $host2" | Out-File $outfile -Append
            # SUB-HOST1
            Test-Connection $sub_host1 -Count 1 | Out-File $outfile -Append
            # SUB-HOST2
            Test-Connection $sub_host2 -Count 1 | Out-File $outfile -Append
            }

        # HOST3
        Test-Connection $host3 -Count 4 | out-null
        if ([int]$? -eq 0)
            {
            $date = Get-Date -Format "HH:mm:ss"
            echo "########################################################" | Out-File $outfile -Append
            echo "$date Lost connection to $host3" | Out-File $outfile -Append
            # SUB-HOST1
            Test-Connection $sub_host1 -Count 1 | Out-File $outfile -Append
            # SUB-HOST2
            Test-Connection $sub_host2 -Count 1 | Out-File $outfile -Append
            }

        }
        while ($asd = 1)

PowerShell Version: 4
Modules: none

Next Page →