Simplify Your Windows 11 Experience: Beginner's Guide to Task Automation with Script Codes
- fabztechtips
- Feb 22, 2024
- 2 min read
Welcome to a more efficient Windows 11 experience! If you've ever felt bogged down by repetitive tasks, it's time to explore the world of automation. In this beginner's guide, we'll delve into simple scripting techniques to automate tasks like emptying the recycle bin and installing pending updates. Fear not, as we'll provide step-by-step examples with script codes to make your Windows 11 journey smoother than ever.
Automating Recycle Bin Cleanup:
Step 1: Open PowerShell ISE
Type "PowerShell ISE" in the Start menu search bar and hit Enter.
Step 2: Write a PowerShell Script to Empty Recycle Bin
Craft a script to delete the contents of the Recycle Bin.
Example: PowerShell script to empty the Recycle Bin every 30 days.
# Example PowerShell Script for Recycle Bin Cleanup
$recycleBin = New-Object -ComObject Shell.Application
$recycleBin.Namespace(0).Items() | ForEach-Object {
$_.InvokeVerb("Delete")
}
# Notify user (optional)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show('Recycle Bin Emptied!', 'Task Completed', 'OK', [System.Windows.Forms.MessageBoxIcon]::Information)
Save the script as .PS1 file to a location that you can easily find
Open Task Scheduler from your Windows start Menu and under the Action tab click Create Task
Click on the Triggers tab and click New
On the New Trigger window select On a schedule next to Begin the task, next to Days chose 30 on the drop-down and select all the months in the Months input, click Ok
Next click on the Actions tab and click New
In the Actions window under Action tab select Start a program then under Program/script click browse and select your PowerShell script you created earlier and click OK
Before clicking ok again don't forget to go to the General tab and name your rule otherwise it will throw an error
Automate Windows Update Installation Powershell Script: To automate windows updates follow the steps above but use the script below $updates = (New-Object -ComObject Microsoft.Update.Session).CreateUpdateSearcher().Search("IsInstalled=0")
if ($updates.Updates.Count -eq 0) {
Write-Host "No pending updates found."
} else {
Write-Host "Installing pending updates..."
$updateSession = New-Object -ComObject Microsoft.Update.Session
$updateInstaller = $updateSession.CreateUpdateInstaller()
$updateInstaller.Updates = $updates.Updates
$installationResult = $updateInstaller.Install()
if ($installationResult.ResultCode -eq 2) {
Write-Host "Updates installed successfully."
} else {
Write-Host "Failed to install updates. Error code: $($installationResult.ResultCode)"
}
} By incorporating these simple script codes into your Windows 11 environment, you've taken the first steps towards a more streamlined and automated experience. Experiment with these examples, and as you become more comfortable, you can explore additional automation possibilities. Happy scripting!
Comments