“Agent terminal wrapper: ‘An item with the same key has already been added’ when capturing env”

Where does the bug appear (feature/product)?

Cursor IDE

Describe the Bug

In the post, include:
Title: e.g. “Agent terminal wrapper: ‘An item with the same key has already been added’ when capturing env”
What happens: When the AI runs a terminal command, the output shows a PowerShell error from the wrapper script: Get-ChildItem Env: | Sort-Object Name fails with “An item with the same key has already been added.”
What you’ve ruled out: Running the same command yourself in Cursor’s terminal works; Get-ChildItem Env: | Group-Object Name | Where-Object { $_.Count -gt 1 } shows no duplicate env vars in your session.
Conclusion: The failure only happens when the agent runs the command (different process/wrapper), not in your normal terminal.
Cursor version: Your Cursor version (e.g. from Help → About).

Steps to Reproduce

above

Expected Behavior

no error reported

Screenshots / Screen Recordings

Operating System

Windows 10/11

Version Information

Version: 2.4.28 (user setup)
VSCode Version: 1.105.1
Commit: f3f5cec40024283013878b50c4f9be4002e0b580
Date: 2026-02-03T00:56:18.293Z
Build Type: Stable
Release Track: Default
Electron: 39.2.7
Chromium: 142.0.7444.235
Node.js: 22.21.1
V8: 14.2.231.21-electron.0
OS: Windows_NT x64 10.0.26200

For AI issues: which model did you use?

?

For AI issues: add Request ID with privacy disabled

?

Does this stop you from using Cursor

No - Cursor works, but with this issue

Hey, thanks for the report. This is a known issue with the PowerShell wrapper script that Cursor uses to run commands through the agent. The script tries to collect environment variables via Get-ChildItem Env:, but on Windows the same variable (Path, TEMP, etc.) can exist in multiple scopes (Process, User, Machine), and the script can’t handle duplicates.

Here’s a workaround that helped other users:

  • Open Cursor Settings Ctrl+Shift+J
  • Go to Agents, find Legacy Terminal Tool
  • Enable it
  • Ctrl+Shift+P → Terminal: Kill All Terminals
  • Fully restart Cursor
  • Try running commands again

Legacy Terminal Tool uses the older implementation, which avoids this wrapper script issue.

Another option is to install PowerShell 7 instead of the built-in Windows PowerShell 5.1. One user fixed it that way: Agent running commands is broken

Let me know if this doesn’t help.

This topic was automatically closed 22 days after the last reply. New replies are no longer allowed.