简要概述:自动化是提升效率的终极方案。根据Microsoft的数据,PowerShell是全球使用最广泛的系统管理脚本语言,月活用户超过4000万。本文基于实际运维经验,从基础语法到实用脚本,帮你掌握Windows自动化的核心技能。

PowerShell ISE — 内置的脚本编辑和调试环境
📌 核心要点
1. PowerShell月活用户超过4000万,是系统管理标准工具
2. Set-ExecutionPolicy RemoteSigned启用脚本执行
3. 任务计划程序实现脚本定时自动运行
4. Power Automate Desktop是免费的图形化自动化工具
5. winget配合脚本可批量安装管理软件
PowerShell vs CMD对比
📊 关键数据
- PowerShell月活用户:超过4000万 — Microsoft 2024
- PowerShell Gallery模块数:超过12000个 — PSGallery
- 自动化节省时间:重复任务效率提升80-95% — Forrester
- Power Automate Desktop用户:超过1000万 — Microsoft 2024
| 特性 | CMD批处理 | PowerShell | 推荐 |
|---|---|---|---|
| 语言类型 | 简单命令 | 完整脚本语言 | PowerShell ✅ |
| 管道传递 | 文本 | 对象 | PowerShell ✅ |
| 远程管理 | 有限 | 完整支持 | PowerShell ✅ |
| 模块扩展 | 无 | 12000+模块 | PowerShell ✅ |
| 学习曲线 | 简单 | 中等 | CMD ✅ |
| 兼容性 | 所有Windows | Win7+ | 相当 |
| 跨平台 | 仅Windows | Win/Mac/Linux | PowerShell ✅ |
PowerShell基础语法
常用命令
# 获取帮助
Get-Help Get-Process -Full
# 查看文件列表
Get-ChildItem -Path C:\Users -Recurse -Filter *.txt
# 查看进程
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10
# 查看服务
Get-Service | Where-Object {$_.Status -eq "Running"}
# 系统信息
Get-ComputerInfo | Select-Object OsName, OsVersion, CsTotalPhysicalMemory
实用自动化脚本
脚本1:批量重命名文件
# 将文件夹中所有jpg文件按序号重命名
$files = Get-ChildItem -Path "C:\Photos" -Filter *.jpg
$counter = 1
foreach ($file in $files) {
$newName = "photo_{0:D4}.jpg" -f $counter
Rename-Item -Path $file.FullName -NewName $newName
$counter++
}
脚本2:定时清理临时文件
# 清理超过7天的临时文件
$tempPaths = @($env:TEMP, "C:\Windows\Temp")
$daysOld = 7
foreach ($path in $tempPaths) {
Get-ChildItem -Path $path -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$daysOld) } |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host "清理完成:已删除超过${daysOld}天的临时文件"
脚本3:磁盘空间监控
# 检查磁盘空间,低于阈值时警告
$threshold = 10 # GB
Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" | ForEach-Object {
$freeGB = [math]::Round($_.FreeSpace / 1GB, 2)
$totalGB = [math]::Round($_.Size / 1GB, 2)
$drive = $_.DeviceID
if ($freeGB -lt $threshold) {
Write-Warning "$drive 剩余空间不足!剩余: ${freeGB}GB / ${totalGB}GB"
} else {
Write-Host "$drive 空间正常: ${freeGB}GB / ${totalGB}GB"
}
}
脚本4:批量安装软件(winget)
# 使用winget批量安装常用软件
$apps = @(
"Google.Chrome",
"Microsoft.VisualStudioCode",
"7zip.7zip",
"Notepad++.Notepad++",
"VideoLAN.VLC"
)
foreach ($app in $apps) {
Write-Host "正在安装: $app"
winget install --id $app --accept-package-agreements --accept-source-agreements
}
任务计划程序配置
图形界面创建定时任务
- Win+R输入taskschd.msc打开任务计划程序
- 点击创建基本任务
- 设置名称和描述
- 选择触发器(每天/每周/开机时/登录时)
- 操作选择启动程序
- 程序:
powershell.exe - 参数:
-ExecutionPolicy Bypass -File "C:\Scripts\cleanup.ps1"
PowerShell创建定时任务
# 创建每天凌晨3点运行的清理任务
$action = New-ScheduledTaskAction -Execute "powershell.exe" `
-Argument '-ExecutionPolicy Bypass -File "C:\Scripts\cleanup.ps1"'
$trigger = New-ScheduledTaskTrigger -Daily -At "3:00AM"
Register-ScheduledTask -TaskName "DailyCleanup" -Action $action -Trigger $trigger `
-Description "每日自动清理临时文件"
自动化工具对比
| 工具 | 类型 | 学习难度 | 适合场景 | 价格 |
|---|---|---|---|---|
| PowerShell | 脚本语言 | 中等 | 系统管理、IT运维 | 免费 |
| Power Automate Desktop | 图形化RPA | 简单 | 办公自动化、网页操作 | 免费 |
| AutoHotkey | 脚本语言 | 简单 | 快捷键、简单自动化 | 免费 |
| Python | 编程语言 | 中等 | 数据处理、复杂自动化 | 免费 |
| 批处理(.bat) | 命令脚本 | 简单 | 简单文件操作 | 免费 |
FAQ
PowerShell和CMD批处理有什么区别?
PowerShell是现代化的脚本语言,基于.NET框架,支持对象管道和模块扩展。CMD功能有限但兼容性好。新脚本建议用PowerShell,简单任务可以用CMD。
PowerShell脚本运行提示禁止执行怎么办?
以管理员身份运行:Set-ExecutionPolicy RemoteSigned,允许运行本地脚本但要求远程脚本有签名。不建议设为Unrestricted。
如何让脚本定时自动运行?
使用任务计划程序(taskschd.msc)。创建任务 → 设置触发器 → 操作选启动程序 → 程序填powershell.exe,参数填-File 脚本路径。
有没有适合新手的自动化工具?
Power Automate Desktop是微软免费的图形化自动化工具,通过拖拽操作创建流程,无需编程基础。AutoHotkey也很流行,适合创建快捷键。
PowerShell能做哪些实用的自动化?
常见场景:批量重命名文件、定时备份、清理临时文件、监控磁盘空间、批量安装软件(winget)、系统信息收集、日志分析等。
想了解更多电脑实用技巧?系统玩家持续更新各类硬件评测和系统优化教程。
原创文章,作者:系统玩家,如若转载,请注明出处:https://www.xitongwanjia.com/edu/fix/powershell.html
微信扫一扫
支付宝扫一扫