itecs blog

keep informed about the latest developments in it support, cybersecurity and cloud computing technolgies

decorative circles
decorative circles

itecs
blog

keep informed about the latest developments in it support, cybersecurity and cloud computing technolgies

Powershell Script image to clean your computer

PowerShell script that clears temp files, logs, and cleans the operating system

January 21, 2023

How to write a Powershell script that cleans your computer

First, PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on top of the .NET Framework. It allows users to automate tasks and manage Windows systems using a powerful and flexible scripting language, rather than the traditional graphical user interface (GUI).

To create a PowerShell script, you can use any text editor such as Notepad, Notepad++, or Visual Studio Code. You will also need to have PowerShell installed on your computer. You can check if it is installed by opening a Command Prompt or PowerShell window and typing "powershell" and press enter.

Here is an example of a simple PowerShell script that displays the current date and time:

-------

# This script displays the current date and time

$currentDate = Get-Date
Write-Host "Current date and time: $currentDate"

-------

You can save this script as a .ps1 file, for example: "current-time.ps1" . To run the script, you can open a PowerShell window, navigate to the directory where the script is saved, and type the name of the script followed by the Enter key.

You can also run the script by right-clicking on the script file and selecting "Run with PowerShell"

It is important to note that PowerShell scripts can be used to perform a wide range of tasks, from simple operations like displaying text on the screen to complex tasks such as managing multiple servers or automating the deployment of software. Before running any script, it's important to understand what it does and how it works, and to test it in a safe environment.

Here is an example of a PowerShell script that clears temp folders, logs, and unnecessary files, including child objects:

------------

# Define the folder paths for temp files and logs

$tempFolders = "C:\Windows\Temp", "C:\Users\*\AppData\Local\Temp"

$logFolders = "C:\Windows\Logs", "C:\ProgramData\Microsoft\Windows\WER\ReportQueue"

# Clear temp files

foreach ($folder in $tempFolders) {

   Get-ChildItem $folder -Recurse -Force | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

}

# Clear logs

foreach ($folder in $logFolders) {

   Get-ChildItem $folder -Recurse -Force | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

}

# Clear unnecessary files

$files = "*.tmp", "*.log", "*.old"

foreach ($file in $files) {

   Get-ChildItem -Path C:\ -Include $file -Recurse -Force | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

}

# Clear recycle bin

$recy = "C:\$Recycle.Bin"

foreach ($recycle in $recy) {

   Get-ChildItem $recycle -Recurse -Force | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

}

# Clear prefetch

$prefetch = "C:\Windows\Prefetch"

foreach ($pref in $prefetch) {

   Get-ChildItem $pref -Recurse -Force | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

}

----------

This script uses the Get-ChildItem cmdlet

to recursively search for files and folders in the specified temp and log folders, and the Remove-Item cmdlet to delete them. It also uses the -Force and -ErrorAction SilentlyContinue parameters to delete files and folders without prompting for confirmation and to ignore any errors that may occur during the deletion process.It also clears recycle bin and prefetch folder, as well as temp files in user's profile folder

Please note that this script deletes files and folders permanently, and it is highly recommended to backup your important files before running the script.

It is also important to test the script in a test environment and make sure it does not break any other functionalities before running it in production environment.

Latest Posts