Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 9 of 30
❮Prev Next❯

Copilot CLI Sessions, Scripts and Files

Module 2: Copilot Features ⏱ 11 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    GitHub Copilot CLI runs two ways. An interactive session (copilot, or copilot -i "…" to start with a prompt) asks before each new tool, and a yes can last for the session; /exit saves it and copilot --continue or --resume brings it back. A non-interactive run (copilot -p "…") does one prompt and exits, so it can't ask: it only uses tools you pre-approve with --allow-tool (such as 'write' or 'shell(git)'), and --deny-tool always wins. Use it to generate scripts and tidy files, then read the result before you run it.

    Simulate it » No Azure account needed

    Exam objective: 2.2.4 Use GitHub Copilot CLI interactively and in sessions. 2.2.5 Generate scripts and manage files with GitHub Copilot CLI. (Skills measured as of August 7, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    VS Code & GitHub: Copilot CLI: scripts, files and permissions One-shot prompts, tool permissions and resuming a session.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading…

    <code>copilot -p "…"</code> runs <strong>one prompt</strong> and exits: good for scripts and CI. It can’t stop to ask, so tools it hasn’t been allowed are skipped.

    👆 Click the glowing item on screen to continue

    Read the steps as text
    1. copilot -p "…" runs one prompt and exits: good for scripts and CI. It can’t stop to ask, so tools it hasn’t been allowed are skipped.
    2. Allow exactly what it needs with --allow-tool and block what it must never do with --deny-tool. A deny always wins. --allow-all-tools exists, but use it only for tasks you trust.
    3. Read the script before you run it. Line 5 deletes old archives: is that what you want? Copilot’s output is a draft.
    4. In an interactive session Copilot asks before each new tool, and a yes lasts for the session. /exit saves the session; copilot --continue (or --resume) picks it up again.

    🧪 Simulation for learning. Screens are simplified versions of Visual Studio Code and github.com with GitHub Copilot; names, files and suggestions are examples. Not affiliated with GitHub or Microsoft, and nothing here touches your real account.

    😟 THE PROBLEM

    You want Copilot in a script, but you don’t want it running anything it likes.

    😊 THE SOLUTION

    Run one-shot prompts with exact tool permissions, and resume sessions when you need them.

    • No human to approve in CI✓ copilot -p with --allow-tool
    • Fear of rm -rf✓ --deny-tool always wins
    • Lost conversation✓ copilot --continue
    🛠️ Solved with: Copilot CLI sessions

    Start with a question

    Would you give a new assistant your house keys and say 'do whatever you think is needed' while you're away?

    Probably not. You’d say “you can water the plants and take in the post, but don’t touch the car”. And if you’re at home, you’d rather they ask before each new job.

    Copilot CLI works exactly like that. In an interactive session you’re at home: it asks before each new tool, like writing a file or running mv. In a non-interactive run (copilot -p) you’re away, so you hand over a written list: --allow-tool 'write', --deny-tool 'shell(rm)'. Anything not on the list, it doesn’t do.

    Explain it like I’m 10

    The technical explanation

    Interactive sessions

    $ copilot                                         # open a session
    $ copilot -i "organize the old log files into logs/archive"   # open one with a first prompt

    In a session, Copilot asks before it uses a tool it hasn’t been allowed:

    ? Allow Copilot to use shell(mv)? (y/n) y
    ✔ Moved 2 files to logs/archive/

    The real prompt lets you approve that tool once or for the rest of the session, or say no and tell Copilot what to do instead (the exact wording changes between versions). Useful session commands:

    • /exit leaves and saves the session. copilot --continue resumes the most recent one; copilot --resume lets you pick. Inside a session, /resume switches and /session shows details.
    • /context shows what’s filling the context window, /compact summarises history to free space, and /clear starts over.
    • /usage shows what the session used, including premium requests.
    • !git status runs a shell command yourself without leaving.

    Non-interactive runs with -p

    copilot -p "…" (or --prompt) runs one prompt and exits. That’s what you want in shell scripts, cron jobs and CI. It can’t stop to ask, so any tool you haven’t pre-approved is refused:

    $ copilot -p "write a bash script that backs up logs/ and keeps 7 archives"
    ✖ Create scripts/backup-logs.sh: needs permission for "write".

    Give it exactly what it needs:

    copilot -p "write a bash script that backs up logs/ and keeps 7 archives" \
      --allow-tool 'write' \
      --deny-tool 'shell(rm)'

    Tool patterns and the deny rule

    PatternMeans
    'write'Create or change files
    'shell'Any shell command
    'shell(git)'Only git commands
    'github(list_issues)'One tool from an MCP server (server name, then tool)

    Two rules matter. --deny-tool always wins over an allow, so --allow-tool 'shell' --deny-tool 'shell(rm)' runs anything except rm. And --allow-all-tools approves everything: fine in a throwaway container you trust, risky on your laptop or in a CI job that holds secrets.

    1. 1 Denied?

      If a --deny-tool pattern matches, the tool is refused. Full stop.

    2. 2 Allowed?

      If --allow-tool or a session approval matches, it runs.

    3. 3 Interactive?

      In a session, Copilot asks you.

    4. 4 Programmatic

      With -p there is nobody to ask, so the tool is refused.

    How Copilot CLI decides whether to use a tool

    Scripts and files: review the draft

    Here’s the kind of script you might get:

    #!/usr/bin/env bash
    set -euo pipefail
    mkdir -p backups
    tar -czf "backups/logs-$(date +%F).tar.gz" logs/
    ls -1t backups/logs-*.tar.gz | tail -n +8 | xargs -r rm --

    It looks fine, but the last line deletes files. Read it, check it keeps seven archives as you asked, run it once against a test folder, then commit it so someone reviews it. The same goes for file moves and renames: check git status afterwards and make sure nothing went missing.

    Architect level Configuration, custom agents and MCP in scripted runs

    Copilot CLI keeps its settings in ~/.copilot/: settings.json, mcp-config.json for MCP servers, and agents/ for personal custom agents. A repository can add its own agents in .github/agents/*.agent.md and instructions in .github/copilot-instructions.md or AGENTS.md, which the CLI reads. In a script you can combine them: copilot --agent=test-writer -p "write tests for src/discount.js" --allow-tool 'write' --model <model>. MCP tools follow the same permission rules, so allow them by name, for example --allow-tool 'github(list_issues)', rather than opening everything.

    Real-world business scenario

    Online retail

    Contoso (fictional)

    The problem
    Contoso wanted a nightly job that writes release notes from merged pull requests. The first attempt used --allow-all-tools on a runner holding deployment secrets, and security blocked it.
    What they used
    A scheduled job running copilot -p with a fine-grained token, --allow-tool 'write' and 'shell(git)', and --deny-tool 'shell(curl)' and 'shell(rm)'. The output goes into a pull request, not straight to main.
    Why that choice
    The job gets exactly the tools it needs, a deny always wins, and a person reviews the notes before they're merged.
    The result
    Release notes now arrive every morning, and the security team approved the job because its permissions fit on one line.

    When to use it and when not to

    ✓ Use it when…

    • Interactive sessions for exploring a repo, tidying files and multi-step work you want to watch.
    • copilot -p in scripts and CI with the smallest set of --allow-tool patterns.
    • --continue or --resume to pick up a long task without explaining it again.

    ✕ Think twice when…

    • --allow-all-tools on a machine with secrets or production access.
    • Running a generated script or bulk file move without reading it first.
    • One endless session for unrelated work. Use /compact, /clear or a new session.

    ↔ Alternatives to consider

    • /delegate to hand a bigger task to Copilot coding agent, which works on its own branch and opens a draft PR.
    • Agent mode in VS Code when you want to see diffs in the editor.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the GH-300 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    A nightly job runs GitHub Copilot CLI with copilot -p to generate a script file. The job log shows that Copilot could not create the file because it needed permission. What should you add to the command?

    Question 2

    Question 2 of 5

    You run the following command: copilot -p "clean up the build output" --allow-tool 'shell' --deny-tool 'shell(rm)'. Copilot attempts to run an rm command. What happens?

    Question 3

    Question 3 of 5

    Yesterday a developer worked through a refactoring in an interactive Copilot CLI session and left it with /exit. Today the developer wants to return to the same conversation with the least effort. Which command should the developer run?

    Question 4

    Question 4 of 5 · Select 2

    A developer is in a long interactive Copilot CLI session. Responses are slowing down and referencing earlier, unrelated work. The developer wants to keep working on the current task. Which TWO slash commands help manage the context? Each correct answer presents a complete solution.

    Question 5

    Question 5 of 5

    Copilot CLI generates a Bash script that archives log files and deletes archives older than seven days. What should the developer do before scheduling the script?

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner What's the difference between copilot and copilot -p?

    Model answer

    Plain copilot opens an interactive session where I chat, approve tools as they come up and keep going. copilot -p runs one prompt and exits, which suits scripts and CI. Because it can't stop to ask, it only uses tools I've allowed on the command line.

    Intermediate You want Copilot CLI to write a file in a CI job but never delete anything. How?

    Model answer

    copilot -p with --allow-tool 'write' and --deny-tool 'shell(rm)'. The deny wins even if something broader is allowed. I avoid --allow-all-tools in CI because then it can run anything the job's account can.

    Interviewer follows up

    What happens if you forget --allow-tool 'write'?

    You

    It can't write the file. In programmatic mode there's nobody to ask, so the tool is refused and it tells you it needed permission.

    Experienced developer A long Copilot CLI session is getting slow and the answers drift. What are your options?

    Model answer

    /context shows what's filling the window. /compact summarises the history so I keep the thread without the bulk. If the topic has changed, /clear or a fresh session is cleaner. For a side task, a sub-agent or /delegate keeps that work out of my main context.

    Scenario Copilot CLI wrote a backup script that deletes old archives. Do you run it?

    Model answer

    Not before I read it. I check what it deletes, whether paths are quoted, and that set -euo pipefail is there. I'd run it once against a test folder, then commit it so the change is reviewed like any other code.

    Quick revision

    Key points

    • copilot starts an interactive session; copilot -i "prompt" starts one with a first prompt.
    • copilot -p "prompt" runs once and exits; it can only use pre-approved tools.
    • --allow-tool and --deny-tool take patterns: 'write', 'shell', 'shell(git)', '<mcp-server>(tool)'. Deny wins.
    • --allow-all-tools approves everything. Use it only where you trust the task and the environment.
    • In a session, approve a tool once or for the rest of the session.
    • /exit saves the session; --continue resumes the latest, --resume picks one; /resume and /session work inside.
    • /context and /compact manage the context window.
    • Generated scripts and file moves are drafts. Read and test them.

    Important terminology

    Interactive session
    A Copilot CLI conversation started with copilot or copilot -i, where it can ask you to approve tools as it goes.
    Programmatic mode
    Running one prompt with copilot -p "..." that exits when done and can't stop to ask for approval.
    --allow-tool
    A flag that pre-approves a tool, for example 'write', 'shell(git)' or an MCP server's tool.
    --deny-tool
    A flag that blocks a tool. A deny always beats an allow.
    --continue
    Resumes the most recent Copilot CLI session. --resume lets you pick an earlier one.
    /compact
    Summarises the conversation so far to free space in the context window without starting over.

    Common mistakes

    • Expecting copilot -p to pause and ask for permission.
    • Using --allow-all-tools in CI.
    • Running a generated script before reading it.

    Exam tips

    • "Run Copilot CLI in a script with no prompts" = copilot -p plus --allow-tool.
    • "Block a command even if other tools are allowed" = --deny-tool (deny wins).
    • "Pick up yesterday's conversation" = copilot --continue or --resume.

    Five quick revision questions

    1. 1.Which flag runs a single prompt non-interactively?Show

      -p (or --prompt)

    2. 2.Which wins, --allow-tool or --deny-tool?Show

      --deny-tool

    3. 3.Which tool pattern allows only git commands?Show

      shell(git)

    4. 4.How do you resume the most recent session?Show

      copilot --continue

    5. 5.Which slash command summarises history to free context space?Show

      /compact

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Agent Mode, MCP Servers and Sub-Agents

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.