Streamline Your Tasks: 6 Essential PowerShell Scripts
Automation is more than a productivity buzzword on Windows—it’s a way to reclaim valuable time. Whether you’re managing files, monitoring disk space, or patching your system, PowerShell offers concise, customizable command-line solutions that often outperform tedious GUI navigation. Let’s dive into six scripts every Windows user (and sysadmin) should consider adding to their toolkit.1. Move Files and Folders Effortlessly
What It Does
Managing files using the traditional File Explorer can be clunky—especially when you’re repeatedly copying and deleting files to free up space. This script uses PowerShell’sMove-Item
cmdlet to transfer files and folders from one location to another in a single command.How It Works
By moving files using the CLI, you bypass the graphical interface delays while automatically deleting files from the source once copied. For example:
Code:
Move-Item -Path "C:\source\path" -Destination "D:\destination\path"
Why It’s Useful
- Speed: CLI operations execute faster than manual drag-and-drop actions.
- Efficiency: Automate regular cleanups or migrations with a single command.
- Customization: Easily adapt the script to your directory structure or specific needs.
2. View and Manage Local User Accounts
What It Does
For administrators of shared Windows 11 PCs, swiftly checking user accounts is vital. Using theGet-LocalUser
cmdlet, you can easily list all user accounts, review their last logon times, and even export this data to a CSV file for further analysis.Script Breakdown
This comprehensive snippet shows several user-management operations:
Code:
# List all local users with key details
Get-LocalUser | Format-Table Name, Enabled, LastLogon
# Get details for a specific user
$specificUser = "Administrator"
Get-LocalUser -Name $specificUser | Format-List Name, FullName, Enabled, LastLogon, PasswordLastSet, PasswordExpires
# Export all local user information to a CSV file
$csvPath = "$env:USERPROFILE\Desktop\LocalUsers.csv"
Get-LocalUser | Select-Object Name, Enabled, LastLogon, PasswordLastSet, PasswordExpires | Export-Csv -Path $csvPath -NoTypeInformation
Why It’s Useful
- Quick Data Retrieval: No need to navigate through Settings or Lusrmgr.
- Reporting: Export functionality helps with regular audits or troubleshooting.
- Flexibility: Modify the cmdlets to display or filter the information critical to your environment.
3. Reinstall the Microsoft Store in Seconds
What It Does
Issues with the Microsoft Store can halt your workflow—whether it’s due to corruption or simply a misbehaving update. Reinstalling the Store using PowerShell bypasses the lengthy manual troubleshooting process.How It Works
By executing two simple commands, you first remove the faulty store package and then reinstall it:
Code:
# Uninstall the Microsoft Store for all users
Get-AppxPackage -allusers *WindowsStore* | Remove-AppxPackage
# Reinstall the Microsoft Store
Get-AppxPackage -AllUsers Microsoft.WindowsStore* | Foreach { Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml" }
Why It’s Useful
- Rapid Resolution: Fix issues without delving into system settings.
- Automation: Integrate into troubleshooting scripts for quicker resolutions in larger deployments.
- Reliability: Restores the Store to its original state for better stability.
4. Automate Disk Space Monitoring
What It Does
Keeping an eye on disk space is crucial, especially if you’re juggling multiple tasks on your system. This script checks the drive space and sends an email alert when the free space dips below a set threshold.How It Works
Using PowerShell to iterate over your drives, this script sends an alert via email if free space falls below 10GB (or any threshold you define):
Code:
$threshold = 10GB
$drives = Get-PSDrive -PSProvider FileSystem
foreach ($drive in $drives) {
if ($drive.Free -lt $threshold) {
Send-MailMessage -To "[email]admin@domain.com[/email]" -From "[email]monitor@domain.com[/email]" -Subject "Disk Space Alert" -Body "Drive $($drive.Name) is running low on space." -SmtpServer "smtp.domain.com"
}
}
Why It’s Useful
- Prevention: Avoid unexpected slowdowns by addressing low disk space before it becomes critical.
- Customization: Easily adjust thresholds and email settings for your environment.
- Automation: Run this script on a schedule to continuously monitor your system.
5. Update Applications Automatically
What It Does
Staying current with app updates is important for both functionality and security. This script automates updates across several packaged sources, including the Microsoft Store, Chocolatey, and Winget.How It Works
The multi-part script checks for updates and processes them if the respective package manager is installed:
Code:
# Update Windows Store apps
function Update-WindowsStoreApps {
Write-Host "Updating Windows Store apps..."
Get-CimInstance -Namespace "Root\cimv2\mdm\dmmap" -ClassName "MDM_EnterpriseModernAppManagement_AppManagement01" | Invoke-CimMethod -MethodName UpdateScanMethod
}
# Update Chocolatey packages
function Update-ChocolateyApps {
if (Get-Command choco -ErrorAction SilentlyContinue) {
Write-Host "Updating Chocolatey packages..."
choco upgrade all -y
} else {
Write-Host "Chocolatey is not installed. Skipping Chocolatey updates."
}
}
# Update Winget packages
function Update-WingetApps {
if (Get-Command winget -ErrorAction SilentlyContinue) {
Write-Host "Updating Winget packages..."
winget upgrade --all
} else {
Write-Host "Winget is not installed. Skipping Winget updates."
}
}
# Execute updates
Write-Host "Starting automated app updates..."
Update-WindowsStoreApps
Update-ChocolateyApps
Update-WingetApps
Write-Host "App update process completed."
Why It’s Useful
- Comprehensive: Covers multiple app sources with one command.
- Time-Saving: Eliminates the need for separate update checks.
- Maintenance: Keeps your software secure and feature-rich without manual intervention.
6. Generate a Running Services CSV Report
What It Does
When your system starts slowing down unexpectedly, identifying resource hogs can be challenging. This script creates a CSV report of all running services, easing the troubleshooting process.How It Works
The command filters running services and exports them to a CSV file:
Code:
Get-Service | Where-Object {$_.Status -eq "Running"} | Export-Csv -Path "C:\RunningServices.csv" -NoTypeInformation
Why It’s Useful
- Efficiency: Quickly generates a detailed list for diagnostics.
- Documentation: Provides a snapshot of system services for later analysis.
- Troubleshooting: Identifies services that might be consuming excessive resources.
Bringing It All Together
These six scripts highlight the power and flexibility of PowerShell as an automation tool for Windows. Not only do they free you from repetitive, time-consuming tasks, but they also empower you to customize and extend their functionality according to your unique workflow.Broader Implications
- Productivity Boost: Automating routine processes means more time for higher-value tasks.
- Cross-Platform Versatility: Thanks to its open-source nature, many PowerShell commands and scripts run smoothly on Windows, macOS, and Linux.
- Security and Maintenance: Regularly automating updates, service checks, and system monitoring can help mitigate security risks and reduce downtime.
A Few Tips for Best Practices
- Run with Caution: Always test scripts in a non-critical environment before deploying them on production machines.
- Customize: Tailor each script to fit your operating environment—for example, adjust disk space thresholds or user parameters.
- Stay Informed: The community regularly shares updated scripts and automation tips. Check out related discussions on WindowsForum for further ideas and enhancements.
Conclusion
PowerShell is not just another command-line tool—it's a gateway to a faster, more efficient Windows experience. From moving files and managing user accounts to automating updates and monitoring disk space, these six scripts serve as a reminder that many routine tasks can be streamlined with just a few well-crafted commands. Embrace automation, experiment with these scripts, and soon you'll find that a few lines of code can transform your daily workflow.Happy scripting, and enjoy a boosted level of productivity on your Windows system!
Source: XDA Developers https://www.xda-developers.com/powershell-scripts-automate-speed-workflow/
Last edited: