
您是否曾发现自己被繁琐的计算机任务所困扰?您并不孤单!好消息是,借助人工智能 (AI) 的帮助,您可以使用 PowerShell 脚本简化和自动化 Windows 任务,从而腾出宝贵的时间来处理更重要的活动。
PowerShell 概述:您的自动化盟友
PowerShell 既是功能强大的命令行 shell,也是嵌入在 Windows 中的综合脚本语言,可提供卓越的管理和自动化解决方案。据Microsoft称,它是一种可以改变您管理计算机上的任务的方式的工具。

使用 PowerShell,您可以编写脚本(包含命令和指令的自给自足文件),以自动执行从基本文件管理到复杂系统操作的所有操作。
对于脚本新手,网上有许多预先编写的 PowerShell 脚本可供使用。例如,PowerShell 脚本存储库提供了 500 多个免费脚本,旨在帮助完成各种任务,包括监控驱动器空间、列出已安装的程序以及按创建日期整理照片。
虽然这些现成的脚本很有价值,但 PowerShell 的真正潜力在于您能够创建适合您特定要求的定制脚本 – 这就是 AI 改变游戏规则的地方!
利用 AI 生成自定义 PowerShell 脚本
想象一下,需要整理从损坏的硬盘中恢复的数千个文件。我没有花几个小时掌握 PowerShell 语法或寻找理想的现有脚本,而是使用 ChatGPT 编写了一个自定义脚本。
对于我的特定需求——对各种类型的文件(如图像、音频和文档)进行排序,我为 AI 设计了一个清晰、简洁的提示:

以下是我确保在 AI 提示中包含的内容:
- 处理不适合任何类别的文件的说明。
- 根据文件的扩展名来组织文件的确切任务。
- 每个组的类别和文件类型的细目分类。
人工智能快速生成了一个有效的 PowerShell 脚本来解决我的文件组织任务:
# Define the source folder as the current directory where the script is executed$sourceFolder = $PSScriptRoot# Define category folders and their associated file extensions$categories = @{ "Images" = @(".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp", ".svg", ".ico", ".raw", ".cr2", ".nef", ".heic", ".psd", ".ai", ".eps") "Documents" = @(".pdf", ".docx", ".doc", ".txt", ".rtf", ".xlsx", ".xls", ".pptx", ".ppt", ".odt", ".ods", ".odp", ".md", ".csv", ".json", ".xml", ".html", ".htm", ".log", ".tex", ".epub", ".mobi") "Audio" = @(".mp3", ".wav", ".aac", ".flac", ".ogg", ".wma", ".m4a", ".mid", ".midi", ".aiff", ".alac", ".ape", ".opus", ".amr", ".au", ".voc") "Video" = @(".mp4", ".avi", ".mov", ".wmv", ".mkv", ".flv", ".webm", ".m4v", ".mpg", ".mpeg", ".3gp", ".3g2", ".ts", ".vob", ".ogv", ".divx", ".asf", ".rm", ".rmvb") "Archives" = @(".zip", ".rar", ".7z", ".tar", ".gz", ".bz2", ".xz", ".cab", ".iso", ".tgz", ".tbz2", ".lzh", ".dmg", ".pkg", ".deb", ".rpm") "Code" = @(".py", ".js", ".html", ".css", ".java", ".c", ".cpp", ".cs", ".php", ".rb", ".go", ".swift", ".sql", ".ps1", ".sh", ".bat", ".vbs", ".ts", ".jsx", ".r", ".pl", ".lua") "Executables" = @(".exe", ".msi", ".app", ".apk", ".dll", ".bin", ".com", ".jar", ".gadget", ".wsf", ".appx", ".appxbundle") "Other" = @()}# Create category folders if they don't existforeach ($category in $categories. Keys) { $categoryPath = Join-Path -Path $sourceFolder -ChildPath $category if (-not (Test-Path -Path $categoryPath)) { New-Item -Path $categoryPath -ItemType Directory | Out-Null Write-Host "Created folder: $category" }} # Get all files in the source folder (non-recursive)$files = Get-ChildItem -Path $sourceFolder -File | Where-Object { $_. Name -ne $MyInvocation. MyCommand. Name } # Counter for moved files$movedFiles = 0$totalFiles = $files. CountWrite-Host "Starting to organize $totalFiles files..." # Process each fileforeach ($file in $files) { $extension = $file. Extension. ToLower() $targetCategory = "Other" # Determine which category the file belongs to foreach ($category in $categories. Keys) { if ($category -eq "Other") { continue } if ($categories[$category] -contains $extension) { $targetCategory = $category break } } # Move the file to the appropriate category folder $targetPath = Join-Path -Path $sourceFolder -ChildPath $targetCategory $targetFile = Join-Path -Path $targetPath -ChildPath $file. Name # Check if target file already exists and handle accordingly if (Test-Path -Path $targetFile) { $baseName = [System. IO. Path]::GetFileNameWithoutExtension($file. Name) $newName = "$baseName`_$(Get-Random)$extension" $targetFile = Join-Path -Path $targetPath -ChildPath $newName } Move-Item -Path $file. FullName -Destination $targetFile $movedFiles++ # Show progress $percentComplete = [math]::Round(($movedFiles / $totalFiles) * 100, 2) Write-Progress -Activity "Organizing Files" -Status "$movedFiles of $totalFiles files processed ($percentComplete%)" -PercentComplete $percentComplete } Write-Host "File organization complete! Moved $movedFiles files into categories."
使用任务计划程序自动执行 PowerShell 脚本
AI 生成的 PowerShell 脚本的魔力并不止于创建;它们还可以设置为自动运行,从而无需人工干预!
由于我经常在长时间使用电脑时忽略休息,因此我编写了一个脚本来提醒我短暂休息。我再次利用人工智能生成了这个提醒脚本,并给出了一个简单的提示:
生成的 PowerShell 脚本完全满足我的需要:
# Script to remind user to take regular screen breaks # Load required assemblies for notifications Add-Type -AssemblyName System. Windows. Forms# Function to show break reminder notification function Show-BreakReminder { $motivationalMessages = @( "Time for a 5-minute break! Rest your eyes and stretch.", "Screen break time! Look at something 20 feet away for 20 seconds.", "Break time! Stand up and move around for 5 minutes.", "Your eyes need a rest! Take 5 minutes away from the screen.", "Productivity hack: A 5-minute break now will boost your focus!" ) # Select a random message $randomMessage = $motivationalMessages | Get-Random # Create and configure the notification $notification = New-Object System. Windows. Forms. NotifyIcon $notification. Icon = [System. Drawing. SystemIcons]::Information $notification. BalloonTipTitle = "Wellness Reminder" $notification. BalloonTipText = $randomMessage $notification. Visible = $true # Show notification for 10 seconds $notification. ShowBalloonTip(10000) # Clean up after a delay Start-Sleep -Seconds 12 $notification. Dispose() } # Display an initial notification Show-BreakReminder Write-Host "Break reminder displayed. Set this script to run hourly using Task Scheduler."
现在我有了脚本,我将其设置为通过 Windows 任务计划程序每小时自动执行一次。操作如下:

首先,我将 AI 生成的脚本保存为.ps1 文件,并通过“开始”菜单访问任务计划程序。我继续创建一个每日触发的新基本任务。

我修改了触发器设置,以便每小时运行一次任务。在任务计划程序库中找到我的任务后,我右键单击它并选择“属性”。在“触发器”选项卡中,我单击“编辑”,选中“每隔:重复任务”选项,并将间隔设置为1 小时。我还在持续时间下选择了“无限期” ,然后单击“确定”保存了更改。

接下来,我通过导航到“操作”选项卡并单击“编辑”来调整“属性”窗口中的操作设置。对于“程序/脚本”字段,我输入的powershell.exe
不是脚本的直接路径。在“添加参数”字段中,我使用了-ExecutionPolicy Bypass -WindowStyle Hidden -File "C:\Users\David\Desktop\eye-saver.ps1"
,它涵盖了执行策略和脚本的完整路径。

保存这些设置后,我通过单击两次“确定”来完成“属性”窗口。结果如何?无缝运行的提醒功能提高了我的工作效率,同时减轻了眼睛疲劳!
使用人工智能创建和自动化 PowerShell 脚本的好处在于它能为您的日常工作带来显著的效率。借助这些简单的命令,您不仅可以促进更好的工作流程,还可以让您的计算机在后台默默地为您完成工作。
所有图片和截图均归功于 David Morelo。
常见问题
1.我需要编程知识来创建 PowerShell 脚本吗?
不,你不需要广泛的编程知识。借助 ChatGPT 等 AI 工具,你只需描述你想要的内容即可生成脚本,让每个人都可以使用。
2.如何自动运行我的 PowerShell 脚本?
您可以使用 Windows 任务计划程序来安排 PowerShell 脚本。这允许它们以指定的时间间隔运行,自动执行您的任务而无需您采取进一步的行动。
3.如果我的 AI 生成的脚本没有按预期工作怎么办?
初始脚本通常需要调整。检查生成的代码,确保路径准确,并调整任何参数。请随时查阅 PowerShell 文档以进行故障排除!
发表回复 ▼