Learn › CI/CD & DevOps

GitHub Actions: Your First Workflow to Lint and Test PowerShell

Create a GitHub Actions workflow that runs PSScriptAnalyzer and Pester on every pull request – YAML structure, triggers, jobs, steps and reading the results.

Beginner 10 min read
By Gopinath Karthikeyan

GitHub Actions runs pipelines directly inside GitHub – no separate server to install. In this guide you’ll add a workflow to your winops-lab repo (from the Git guide) that checks every PowerShell script on every pull request.

The building blocks

Term Meaning
Workflow A YAML file in .github/workflows/
Trigger (on) What starts it: push, pull request, schedule, manual button
Job A set of steps that runs on one machine (a runner)
Runner The machine: GitHub-hosted (windows-latest, ubuntu-latest) or your own (self-hosted)
Step One command (run:) or a ready-made action (uses:)

Your first workflow

Create .github/workflows/powershell-ci.yml:

name: PowerShell CI

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]
  workflow_dispatch:        # adds a "Run workflow" button

jobs:
  lint-and-test:
    runs-on: windows-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v4

      - name: Install PSScriptAnalyzer and Pester
        shell: pwsh
        run: |
          Set-PSRepository PSGallery -InstallationPolicy Trusted
          Install-Module PSScriptAnalyzer, Pester -Scope CurrentUser -Force

      - name: Lint scripts
        shell: pwsh
        run: |
          $issues = Invoke-ScriptAnalyzer -Path ./scripts -Recurse -Severity Warning, Error
          $issues | Format-Table -AutoSize
          if ($issues) { throw "$($issues.Count) analyzer issue(s) found" }

      - name: Run Pester tests
        shell: pwsh
        run: |
          if (Test-Path ./tests) {
            $result = Invoke-Pester -Path ./tests -PassThru
            if ($result.FailedCount -gt 0) { throw "$($result.FailedCount) test(s) failed" }
          } else {
            Write-Host 'No tests folder yet'
          }

Commit it on a branch, push, and open a pull request.

Watch it run

  • The PR shows a yellow dot, then a green tick or red cross next to the check.
  • Click Details (or the repo’s Actions tab) to see each step’s log.
  • A failing step stops the job and marks the check red.

Now add the check as required in your branch protection rule (see the PR guide) – broken scripts can no longer merge.

Useful additions

Run on a schedule (e.g. a nightly report):

on:
  schedule:
    - cron: '30 1 * * *'     # 01:30 UTC every day

Test on Windows PowerShell 5.1 as well as PowerShell 7 with a matrix:

jobs:
  test:
    runs-on: windows-latest
    strategy:
      matrix:
        shell: [pwsh, powershell]
    steps:
      - uses: actions/checkout@v4
      - name: Show version
        shell: ${{ matrix.shell }}
        run: $PSVersionTable.PSVersion

Save a report as a downloadable artifact:

      - name: Upload report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: analyzer-report
          path: reports/

Secrets

Repository Settings → Secrets and variables → Actions → New repository secret. Use them as ${{ secrets.NAME }} and pass them to scripts as environment variables. GitHub masks them in logs. Never echo them.

GitHub-hosted runners can’t reach your servers

Hosted runners run in GitHub’s cloud – perfect for linting and tests, but they can’t reach servers inside your network. For deployments, use a self-hosted runner inside your network – see self-hosted runners for Windows automation.

What’s next

Next: Self-hosted runners for Windows automation.

Practise this topic

Test yourself with free questions on this topic – instant answers and explanations.

Start practice questions

GitHub ActionsCI/CDPowerShellPesterBeginner

Gopinath Karthikeyan

Gopinath Karthikeyan – Windows Site Reliability Engineer with about 12 years in enterprise Windows Server infrastructure, and founder of WinOps Academy. YouTube · LinkedIn

More in CI/CD & DevOps