Automate Windows 11 Tasks with PowerShell: A Practical Guide

Automate Windows 11 Tasks with PowerShellAutomate Windows 11 Tasks with PowerShell
Automate Windows 11 Tasks with PowerShell: A Practical Guide

Do you want to save time and effort by automating various tasks on your Windows 11 system? If so, you should learn how to use PowerShell, a powerful scripting language and command-line tool that can help you do it faster and easier. In this blog, we will show you some practical examples of how to use PowerShell to automate common or complex tasks on Windows 11, such as:

  • Managing network settings and connections with PowerShell
  • Monitoring system performance and resources with PowerShell
  • Backing up and restoring files and folders with PowerShell
  • Installing and updating Windows features with PowerShell
  • Creating and running scheduled tasks with PowerShell

Managing network settings and connections with PowerShell

PowerShell can help you manage your network settings and connections on your Windows 11 system with ease and efficiency. You can use PowerShell to perform various operations, such as configuring your IP address, DNS server, firewall, proxy, and VPN. You can also use PowerShell to test your network connectivity, ping, traceroute, and resolve host names.

# Define the interface alias, IP address, subnet mask, gateway, and DNS server
$interface = "Ethernet"
$ip = "192.168.1.100"
$subnet = "255.255.255.0"
$gateway = "192.168.1.1"
$dns = "8.8.8.8"

# Set the IP address, DNS server, and firewall profile configuration of the interface
Set-NetIPAddress -InterfaceAlias $interface -IPAddress $ip -PrefixLength $subnet -DefaultGateway $gateway
Set-DnsClientServerAddress -InterfaceAlias $interface -ServerAddresses $dns
Set-NetFirewallProfile -Profile Private -Enabled True

Monitoring system performance and resources with PowerShell

PowerShell can help you monitor your system performance and resources on your Windows 11 system with ease and efficiency. You can use PowerShell to perform various operations, such as getting the CPU, memory, disk, and network usage, measuring the execution time and memory consumption of a command or script, and generating performance reports and charts.

# PowerShell script for monitoring system performance
# Define the performance counters for CPU and memory usage
$cpu = "\Processor(_Total)\% Processor Time"
$memory = "\Memory\Available MBytes"

# Get the performance counter data for CPU and memory usage
$data = Get-Counter -Counter $cpu,$memory -SampleInterval 1 -MaxSamples 10

# Create a chart object from the performance counter data
$chart = New-Object System.Windows.Forms.DataVisualization.Charting.Chart
$chart.Width = 800
$chart.Height = 600
$chart.BackColor = "White"

# Add a chart area, a series for CPU usage, a series for memory usage, and a legend to the chart object
$area = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$area.AxisX.Title = "Time (seconds)"
$area.AxisY.Title = "Usage (%)"
$area.AxisY2.Title = "Available (MB)"
$chart.ChartAreas.Add($area)

$series1 = New-Object System.Windows.Forms.DataVisualization.Charting.Series
$series1.Name = "CPU"
$series1.ChartType = "Line"
$series1.Color = "Red"
$series1.BorderWidth = 3
$series1.Points.DataBindXY($data.Timestamps,$data.CounterSamples[0].CookedValue)
$chart.Series.Add($series1)

$series2 = New-Object System.Windows.Forms.DataVisualization.Charting.Series
$series2.Name = "Memory"
$series2.ChartType = "Line"
$series2.Color = "Blue"
$series2.BorderWidth = 3
$series2.YAxisType = "Secondary"
$series2.Points.DataBindXY($data.Timestamps,$data.CounterSamples[1].CookedValue)
$chart.Series.Add($series2)

$legend = New-Object System.Windows.Forms.DataVisualization.Charting.Legend
$legend.Docking = "Top"
$chart.Legends.Add($legend)

# Save the chart object as an image file
$chart.SaveImage("C:\Performance.png","png")

Backing up and restoring files and folders with PowerShell

PowerShell can help you backup and restore your files and folders on your Windows 11 system with ease and efficiency. You can use PowerShell to perform various operations, such as creating, copying, moving, renaming, deleting, searching, and compressing files and folders. You can also use PowerShell to create and use backup policies, backup sets, and backup items.

# PowerShell script for backing up and restoring files and folders
# Define the folder to backup and the backup location
$folder = "C:\Users\YourName\Documents"
$location = "D:\Backup"

# Create a backup policy that runs daily and keeps backups for 30 days
$policy = New-BackupPolicy -Frequency Daily -RetentionPeriod 30

# Set the backup policy for the computer
Set-BackupPolicy -Policy $policy

# Backup the folder to the backup location
Backup-File -Source $folder -Destination $location

Here is a summary of a PowerShell script that restores a file or folder from a backup to a specified location:

# Define the file or folder to restore and the restore location
$file = "C:\Users\YourName\Documents\Report.docx"
$location = "C:\Users\YourName\Desktop"

# Get the latest backup set that contains the file or folder
$set = Get-BackupSet | Sort-Object -Property CreationTime -Descending | Select-Object -First 1

# Get the backup item that matches the file or folder
$item = Get-BackupItem -BackupSet $set -Path $file

# Restore the file or folder to the restore location
Restore-File -BackupItem $item -Destination $location

    

Installing and updating Windows features with PowerShell

PowerShell can help you install and update Windows features on your Windows 11 system with ease and efficiency. You can use PowerShell to perform various operations, such as listing, enabling, disabling, or updating Windows features, such as Hyper-V, Windows Subsystem for Linux, or Windows Sandbox.

# PowerShell script for installing and updating Windows features
# Install the Hyper-V feature on the computer
Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Restart

Here is a summary of a PowerShell script that uninstalls the Windows Subsystem for Linux feature from a computer and removes the related files:

# Uninstall the Windows Subsystem for Linux feature from the computer
Uninstall-WindowsFeature -Name Microsoft-Windows-Subsystem-Linux -Remove

Creating and running scheduled tasks with PowerShell

PowerShell can help you create and run scheduled tasks on your Windows 11 system with ease and efficiency. You can use PowerShell to perform various operations, such as creating, registering, starting, stopping, or disabling scheduled tasks, such as running a PowerShell script, sending an email, or displaying a message.

# PowerShell script for creating and running scheduled tasks
# Define the PowerShell script to run
$script = "C:\Scripts\Backup.ps1"

# Create a new scheduled task action that runs the PowerShell script
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File $script"

# Create a new scheduled task trigger that runs the task every day at 10 AM
$trigger = New-ScheduledTaskTrigger -Daily -At 10am

# Create a new scheduled task setting that stops the task if it runs too long
$setting = New-ScheduledTaskSettingSet -ExecutionTimeLimit (New-TimeSpan -Minutes 30)

# Register the new scheduled task on the computer with the name, action, trigger, and setting
Register-ScheduledTask -Name "Backup" -Action $action -Trigger $trigger -Setting $setting

Conclusion

PowerShell is a versatile and powerful tool that can help you automate various tasks on your Windows 11 system. You can find more information and examples about PowerShell in the official PowerShell documentation. Thank you for reading this blog post. I hope you found it helpful and interesting. 😊

Leave a Reply

Please disable your adblocker or whitelist this site!