(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to generate automation scripts with Microsoft Copilot: step by step

João Barros 28 de July de 2026 5 min read

Saving time by automating repetitive tasks is a major advantage. This guide shows how to generate an automation script with Microsoft Copilot to organize files in a folder based on type and date, useful for file management and backups.

Prerequisites

  • Account with access to Microsoft Copilot (Copilot integrated in Microsoft 365 or Copilot for developers).
  • Environment to run scripts: Windows 10/11 with PowerShell or macOS/Linux with Bash (choose one).
  • Text editor (VS Code recommended) and permissions to read/write in the target folder.
  • Basic knowledge of PowerShell or Bash commands (introductory level).

Step 1: Define a clear objective for Copilot

Explain to Copilot exactly what you want: move files by type (e.g.: .pdf, .docx, .jpg) into subfolders named after the extension and, additionally, group by year of modification. A clear request avoids vague responses.

Prompt example:
"Create a PowerShell script that walks the folder C:\Users\MeuUtilizador\Downloads, creates subfolders by extension (.pdf, .docx, .jpg) and, inside each subfolder, creates subfolders by year of modification (e.g.: 2024). Move the corresponding files. Include simple logging and error handling."

Step 2: Choose language and request minimum version

Specify the language you will use (PowerShell for Windows, Bash for macOS/Linux) and request a minimum version, for compatibility. This avoids incompatible commands.

Prompt example for PowerShell:
"Generate a PowerShell script compatible with PowerShell 7+.
- Input: folder path as variable $SourcePath.
- Create subfolders by extension and by year of modification.
- Log to file move-log.txt.
- Include file existence checks and error handling."

Step 3: Ask for iterative improvements and explain why

When Copilot generates a first draft, ask for improvements: performance (use Get-ChildItem with -File), safety (avoid overwriting) and progress messages. Explain why you want each change to get code better aligned with your scenario.

Iteration prompt:
"Optimize for performance and avoid overwriting existing files (if present, add a suffix with a counter). Add Write-Progress for visibility."

Step 4: Test the generated script in a controlled environment

Before running in the real folder, create a test folder with some sample files. Run the script in 'dry-run' mode if Copilot includes that option, or add a $WhatIf variable to simulate moves.

# Minimal PowerShell example for dry-run
$SourcePath = "C:\Temp\TestFiles"
$WhatIf = $true  # set to $false to execute

# The main script should respect $WhatIf and use -WhatIf where applicable

Step 5: Review security and permissions

Verify that the script does not run commands with elevated privileges unnecessarily and that it uses validated absolute paths. Ask Copilot to add path validation and error logging with try/catch.

Validation prompt:
"Add validation: confirm that $SourcePath exists and is a folder. In case of error, write to the log and exit with error code 1."

Step 6: Final example (functional PowerShell)

Here is a minimal, functional example you can use and adapt. Replace the path with the one you intend to use.

# Script PowerShell example (PowerShell 7+)
param(
    [string]$SourcePath = "C:\Users\MeuUtilizador\Downloads",
    [switch]$WhatIf
)

$LogPath = Join-Path -Path $SourcePath -ChildPath "move-log.txt"

if (-not (Test-Path -Path $SourcePath -PathType Container)) {
    "[$(Get-Date)] Error: Folder not found: $SourcePath" | Out-File -FilePath $LogPath -Append
    exit 1
}

Get-ChildItem -Path $SourcePath -File -Recurse | ForEach-Object -Begin { $i = 0 } -Process {
    $i++
    $ext = ($_.Extension -replace '^\.', '').ToLower()
    if ([string]::IsNullOrEmpty($ext)) { $ext = 'noext' }
    $year = $_.LastWriteTime.Year

    $destFolder = Join-Path -Path $SourcePath -ChildPath (Join-Path -Path $ext -ChildPath $year)
    if (-not (Test-Path -Path $destFolder)) { New-Item -Path $destFolder -ItemType Directory | Out-Null }

    $destPath = Join-Path -Path $destFolder -ChildPath $_.Name

    # Avoid overwriting: add suffix if exists
    $count = 0
    $baseName = [System.IO.Path]::GetFileNameWithoutExtension($destPath)
    $extension = [System.IO.Path]::GetExtension($destPath)
    while (Test-Path -Path $destPath) {
        $count++
        $destPath = Join-Path -Path $destFolder -ChildPath ("{0}({1}){2}" -f $baseName, $count, $extension)
    }

    if ($WhatIf) {
        "WhatIf: Move '$($_.FullName)' to '$destPath'" | Out-File -FilePath $LogPath -Append
    } else {
        try {
            Move-Item -Path $_.FullName -Destination $destPath -ErrorAction Stop
            "[$(Get-Date)] Moved: '$($_.FullName)' -> '$destPath'" | Out-File -FilePath $LogPath -Append
        } catch {
            "[$(Get-Date)] Error moving '$($_.FullName)': $_" | Out-File -FilePath $LogPath -Append
        }
    }
}

"[$(Get-Date)] Completed. Processed: $i files." | Out-File -FilePath $LogPath -Append

Verify the result

Confirm that the folder structure was created (by type and year) and that the files were moved without loss. Open the move-log.txt file to consult the operation and error history. If you used $WhatIf first, check only the 'WhatIf' entries.

Conclusion

Using Microsoft Copilot to generate scripts speeds up development and reduces manual errors. As a next step, try adapting the script for filters (by size, by date) or integrate with Power Automate for automatic triggering. Tip: always keep a test copy before running scripts on real data.