Use GitHub Copilot to Write PowerShell Scripts in Real Time

Tool:GitHub Copilot
AI Feature:Inline Code Suggestions
Time:15 minutes setup
Difficulty:Beginner

What This Does

GitHub Copilot watches what you're writing in VS Code and suggests the next line — or the entire script — based on your comment describing what you want. Instead of Googling PowerShell syntax and copy-pasting fragments together, you describe what you need and Copilot writes it as you type.

Before You Start

  • A GitHub account (free at github.com)
  • GitHub Copilot subscription ($10/month — sign up at github.com/features/copilot)
  • VS Code installed (free at code.visualstudio.com)
  • GitHub Copilot extension installed in VS Code
  • Time needed: 15 minutes to install and set up; then immediate use
  • Cost: $10/month (GitHub Copilot Individual)

Steps

1. Install VS Code and the GitHub Copilot Extension

If you don't have VS Code: go to code.visualstudio.com and download for Windows. Install it.

Open VS Code. Click the Extensions icon in the left sidebar (looks like 4 squares). Search "GitHub Copilot." Click Install on the "GitHub Copilot" extension by GitHub. A second extension "GitHub Copilot Chat" is also recommended — install that too.

What you should see: After installation, a GitHub icon appears in your left sidebar and a Copilot status icon appears in the bottom status bar.

2. Sign In with Your GitHub Account

Click the GitHub Copilot icon in the sidebar. Click "Sign in to GitHub." Your browser opens to GitHub's auth page. Authorize VS Code. Return to VS Code — you're now authenticated.

What you should see: The Copilot status bar shows your username, not an error. You're ready.

Troubleshooting: If sign-in fails, ensure your GitHub account has an active Copilot subscription at github.com/settings/copilot.

3. Open a New PowerShell File

In VS Code, File → New File. Save it as script.ps1 (the .ps1 extension tells Copilot this is PowerShell). Now Copilot knows what language you're working in.

4. Write a Comment Describing What You Want

Type a # comment at the top of the file describing what the script should do. Be specific. Then press Enter and start typing $ or any code — Copilot will suggest the rest.

Example comment:

Copy and paste this
# Get all Active Directory users who haven't logged in for 90+ days
# Output: CSV file with Name, SamAccountName, LastLogonDate
# Include error handling if AD module isn't available

Press Enter after the comment. Start typing — Copilot shows a gray inline suggestion. Press Tab to accept it.

What you should see: Gray text suggesting the next line of code. Keep pressing Tab to accept suggestions line by line, or press Escape to skip a suggestion and write your own.

5. Review and Test the Script

Once you have a full script, read through it. Look for:

  • Does it do what you intended?
  • Are there any variables or paths you need to customize?
  • Is there error handling?

Test with -WhatIf first if the script modifies anything:

Copy and paste this
Get-ADUser ... | Disable-ADAccount -WhatIf

This shows what would happen without actually doing it.

Real Example

Scenario: The security team asks you for a list of all service accounts in Active Directory that haven't had their passwords changed in over 365 days.

What you type:

Copy and paste this
# Get all service accounts in AD (accounts whose names start with "svc-")
# Check when password was last set
# Output to CSV: AccountName, PasswordLastSet, DaysSinceChange
# Export to C:\Reports\stale-service-accounts.csv

What Copilot writes: A complete script using Get-ADUser with the right filters, Select-Object with calculated fields, and Export-Csv. The script includes error handling and a completion message.

What you do: Review the script, verify the AD filter matches your org's naming convention, run in -WhatIf mode, then run for real.

Time saved: Manual research + writing: 30-60 min. With Copilot: 5-10 min.

Tips

  • The more specific your comment, the better the suggestion — "get AD users" gives a generic script; "get enabled AD users in the Marketing OU whose last logon is over 30 days ago" gives exactly what you need
  • Use the Copilot Chat panel (bottom of VS Code) to ask questions: "Explain what this script does" or "Add error handling to this section"
  • If a suggestion is wrong, type the correct version — Copilot learns from your edits within the session

Tool interfaces change — if a button has moved, look for similar AI/magic/smart options in the same menu area.