
您是否發現自己被繁瑣的電腦任務所困擾?你並不孤單!好消息是,借助人工智慧 (AI) 的幫助下,您可以使用 PowerShell 腳本簡化和自動化 Windows 任務,從而騰出寶貴的時間來執行更重要的活動。
PowerShell 概述:您的自動化盟友
PowerShell 既是強大的命令列 shell,也是 Windows 中嵌入的全面腳本語言,提供了卓越的管理和自動化解決方案。據微軟稱,這是一種可以改變你管理電腦任務方式的工具。

使用 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 文件以進行故障排除!
發佈留言 ▼