AI生成スクリプトを使用した自動化により反復的なタスクを排除

AI生成スクリプトを使用した自動化により反復的なタスクを排除

退屈なコンピューター タスクにうんざりしたことはありませんか? あなただけではありません。朗報は、人工知能 (AI) のちょっとした支援により、PowerShell スクリプトを使用して Windows タスクを効率化および自動化し、貴重な時間をより重要なアクティビティに充てることができるということです。

PowerShell の概要: 自動化の味方

PowerShell は、強力なコマンドライン シェルと、Windows に組み込まれた包括的なスクリプト言語の両方として機能し、優れた管理および自動化ソリューションを提供します。Microsoft によるとこれはコンピューター上でのタスクの管理方法を変革できるツールです。

PowerShell インターフェース

PowerShell を使用すると、スクリプト (コマンドと命令が記述された自己完結型のファイル) を作成して、基本的なファイル管理から複雑なシステム操作まですべてを自動化できます。

スクリプト作成の初心者向けに、多数の事前に作成された PowerShell スクリプトがオンラインで提供されています。たとえば、PowerShell スクリプト リポジトリには、ドライブ容量の監視インストールされているプログラムの一覧表示、作成日による写真の整理など、さまざまなタスクを支援するために設計された 500 を超える無料スクリプトが用意されています。

すぐに使用できるこれらのスクリプトは貴重ですが、PowerShell の真の可能性は、特定の要件に合わせてカスタマイズされたスクリプトを作成できることにあります。これが AI がゲームチェンジャーとなる部分です。

AI を活用してカスタム PowerShell スクリプトを生成する

破損したハードドライブから復元した何千ものファイルを整理する必要があると想像してください。PowerShell 構文を習得するのに何時間も費やしたり、理想的な既存のスクリプトを探し回ったりする代わりに、ChatGPT を使用してカスタム スクリプトを作成しました。

私の特定のニーズ、つまりさまざまな種類のファイル(画像、音声、ドキュメントなど)を分類するために、AI に明確で簡潔なプロンプトを作成しました。

PowerShell の AI プロンプト

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 スクリプトの魔法は、作成だけでは終わりません。スクリプトは自動的に実行されるように設定することもできるため、人間の介入が不要になります。

長時間のコンピューター作業中に休憩を忘れてしまうことが多いので、短い休憩を取るよう通知するスクリプトを作成しました。今回も AI を利用して、簡単なプロンプトでこのリマインダー スクリプトを生成しました。

休憩リマインダーのAIプロンプト

結果として得られた 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 タスク スケジューラを使用して 1 時間ごとに自動的に実行されるように設定しました。手順は次のとおりです。

タスクスケジューラ基本タスク

まず、AI 生成スクリプトを.ps1 ファイルとして保存し、スタート メニューからタスク スケジューラにアクセスしました。そして、毎日実行される新しい基本タスクを作成しました。

タスク スケジューラのトリガーを編集する

トリガー設定を変更して、タスクを 1 時間ごとに実行するようにしました。タスク スケジューラ ライブラリでタスクを見つけたら、それを右クリックして [プロパティ]を選択しました。[トリガー]タブで[編集]をクリックし、 [タスクを繰り返す間隔: ] オプションをオンにして、間隔を1 時間に設定しました。また、[期間] で[無期限]を選択し、 [OK]をクリックして変更を保存しました。

タスク スケジューラのトリガー プロパティ

次に、 [アクション]タブに移動して[編集]をクリックし、プロパティ ウィンドウでアクション設定を調整しました。[プログラム/スクリプト] フィールドには、powershell.exeスクリプトへの直接パスではなく、入力しました。[引数の追加] フィールドでは、-ExecutionPolicy Bypass -WindowStyle Hidden -File "C:\Users\David\Desktop\eye-saver.ps1"実行ポリシーとスクリプトへの完全なパスの両方をカバーする を使用しました。

Eye Saver PowerShell スクリプトのセットアップ

これらの設定を保存した後、 [OK]を 2 回クリックしてプロパティ ウィンドウを確定しました。結果は? 目の疲れを軽減しながら生産性を向上させる、シームレスに実行されるリマインダーです。

PowerShell スクリプトの作成と自動化に AI を採用することの利点は、日常業務に驚くべき効率をもたらすことです。これらのシンプルなコマンドを使用すると、ワークフローが改善されるだけでなく、コンピューターがバックグラウンドで静かに作業を実行できるようになります。

すべての画像とスクリーンショットの著作権は David Morelo に帰属します。

よくある質問

1. PowerShell スクリプトを作成するにはプログラミングの知識が必要ですか?

いいえ、高度なプログラミング知識は必要ありません。ChatGPT のような AI ツールを使用すると、必要なものを説明するだけでスクリプトを生成でき、誰でもアクセスできるようになります。

2. PowerShell スクリプトを自動的に実行するにはどうすればよいですか?

Windows タスク スケジューラを使用して PowerShell スクリプトをスケジュールできます。これにより、スクリプトを指定された間隔で実行し、追加の操作なしでタスクを自動化できます。

3. AI によって生成されたスクリプトが期待どおりに動作しない場合はどうなりますか?

初期のスクリプトでは調整が必要になることがよくあります。生成されたコードを確認し、パスが正確であることを確認し、パラメーターを微調整します。トラブルシューティングについては、PowerShell のドキュメントを参照してください。

出典と画像

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です