How can I prevent cursor from making the same mistakes when executing PowerShell?

There are always errors when executing commands in Windows PowerShell. .cursorrules does not make the commands effective. AI will automatically repair the command statements after each error.

For example, it has a wrong line break or wrong encoding. It has to be corrected every time it executes the command itself, wasting a lot of time.

Hey, you may be able to make a Project Rules rule that explains how to structure the Powershell commands:

Go rules the YOLO mode on the feature settings

Yolo mode doesn’t work, it just pauses and asks me to click to confirm the wrong command line

Can anyone show me a correct example?

i tried ,but the command line generted by composer just seem ignore what i meantioned.

I am not sure if you figured it out, but I had the same issue. Here is how i resolved it. I did two things.

  1. I set the default terminal on within Preferences → VC Code Settings → “terminal.integrated.defaultProfile.windows”: “Command Prompt”

  1. I created a project rule specifically for this…

This is the rules i crafted using claude 3.7 based on my current configuration and enviroment… you can ask these types of questions in Ask in the Chat on Cursor.

Terminal Command Rules for Windows Environment

Description

These rules guide Cursor’s AI when generating terminal commands for a Windows-only environment. Due to execution problems with PowerShell, we need to ensure only CMD commands are used.

Rules

  1. NEVER use PowerShell commands in terminal suggestions.

  2. Always use Windows CMD commands (batch commands) for all operations.

  3. Use standard Windows CMD syntax with commands like “dir”, “type”, “copy”, “move”, “del”, etc.

  4. For package installation, use npm, yarn, or other package managers with their CMD-compatible syntax.

  5. Avoid commands that begin with “ps”, “pwsh”, or contain PowerShell syntax like “$_”, “-ErrorAction”, etc.

  6. Use Windows environment variables with %VARIABLE% syntax instead of PowerShell’s $env:VARIABLE.

  7. For file path references, use Windows-style paths with backslashes (C:\path\to\file).

  8. When suggesting command chaining, use “&” or “&&” instead of PowerShell’s pipe operator.

  9. For conditional operations, use “if” with CMD syntax instead of PowerShell conditionals.

  10. Use “for” loops with CMD syntax rather than PowerShell’s foreach.

  11. Use “findstr” instead of PowerShell’s Select-String for text searching.

  12. For redirecting output, use “>” and “>>” operators rather than PowerShell’s Out-File.

  13. When suggesting commands to manipulate JSON or perform complex operations, recommend using Node.js scripts or other command-line tools that work well in CMD.

I have raised this before in the forum. Using .cmd could help maybe but doesn’t address the underlying problem, and also means you lose out of the benefits of using powershell compared with cmd.

My “rules for AI” has a comment to say - make sure you check to see which terminal you are using. and a reminder about the most common mistake of using && instead of ;

There is a general problem whereby the agent does not realise its context - i.e. it often thinks it is using a linux shell.

The way to address this would be an improved system prompt in cursor, smarter models, and potentially better/more fine tuning during training to use different kinds of terminals, better contextual awareness.

1 Like

thx dirkdd ,I will try later ~

1 Like

thx soorfett,I will try later ~

Hey Dirkdd ,it work !
Can i ask u why dont u use powershell instead cmd ? is any problem in powershell when dev?

Soorfett , sometime it work,sometime not ,sad !

I had some weird issues with PowerShell which I think I have now fixed:

Issue

AI Agents in Cursor were unable to capture output from PowerShell commands, resulting in exit code 1 and no visible output, despite commands executing successfully when run manually.

Environment

  • Windows 11 Pro N (Version 10.0.26100)
  • PowerShell 7.5.0 (installed via WinGet)
  • Cursor AI IDE

Solution Steps

  1. Verify PowerShell Installations

    • Confirmed Windows PowerShell 5.1 at: C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
    • Confirmed PowerShell 7.5.0 at: C:\Users\<removed>\AppData\Local\Microsoft\WinGet\Links\pwsh.exe
  2. Configure Cursor Settings

    • Open Cursor Settings (JSON)
    • Location: %APPDATA%\Cursor\User\settings.json
    • Add PowerShell 7 profile and set as default:
    {
        "terminal.integrated.profiles.windows": {
            "PowerShell 7": {
                "path": "C:\\Users\\<removed>\AppData\\Local\\Microsoft\\WinGet\\Links\\pwsh.exe",
                "icon": "terminal-powershell",
                "args": []
            }
        },
        "terminal.integrated.defaultProfile.windows": "PowerShell 7"
    }
    
  3. Terminal Preview Setting

    • Disabled “Use preview box for terminal Ctrl+K” in Cursor settings
    • This setting affects how responses are streamed to the shell
  4. Restart Required

    • Close all terminal instances
    • Restart Cursor to apply changes

Verification

After implementing these changes:

  • PowerShell 7.5.0 runs as the default shell
  • AI Agents can successfully:
    • Execute PowerShell commands
    • Capture command output
    • Use advanced PowerShell 7 features
    • Process pipeline operations
    • Handle object manipulation and formatting

Notes

  • The fix ensures AI Agents can fully interact with PowerShell 7
  • Commands now return proper exit codes and visible output
  • Terminal integration supports all PowerShell features including advanced formatting and object manipulation
  • Some PSReadLine diagnostic messages may appear but don’t affect functionality

P.S. there are a lot of options in Settings when you search for terminal.integrated - I think that others with different issues might find something in there to help

2 Likes

i will try ur solution later,thx Reeky

I have updated my setup recently to fix the powershell issues. Note: I do have Powershell 7.5 installed and set as the default terminal in VC Settings.

Here is the new rule that I created that seems to be working well for me, it is set to be Always Applied

# Windows PowerShell Command Rules

This rule ensures that all shell commands are compatible with Windows PowerShell 7.5.

## Command Replacements

When suggesting shell commands, use these PowerShell equivalents:

- Use `New-Item -ItemType Directory -Path` instead of `mkdir`

- Use `Remove-Item` instead of `rm`

- Use `Remove-Item -Recurse -Force` instead of `rm -rf`

- Use `New-Item -ItemType File -Path` instead of `touch`

- Use `Get-ChildItem` instead of `ls`

- Use `Copy-Item` instead of `cp`

- Use `Move-Item` instead of `mv`

- Use `Get-Content` instead of `cat`

- Use `Write-Output` instead of `echo`

- Use `$PWD` instead of `pwd`

- Use `Set-Location` instead of `cd`

- Use `Select-String` instead of `grep`

- Use `Set-ItemProperty` instead of `chmod`

- Use `$env:` prefix for environment variables

- Use `.` instead of `source`

- Use `Get-Command` instead of `which`

- Use `New-Item -ItemType SymbolicLink -Path` instead of `ln -s`

## Path Conventions

- Use Windows-style path separators (backslashes)

- Convert forward slashes to backslashes in paths

- Use semicolons (;) for path separators in environment variables

## Environment Variables

- Always use `$env:` prefix for environment variables

- Example: `$env:PATH` not `$PATH`

## File Operations

- Use Windows-style paths

- Use proper PowerShell cmdlets for file operations

- Ensure proper error handling with PowerShell try/catch blocks

## Script Execution

- Use `npm run` for npm scripts

- Use `yarn` for yarn commands

- Use `pnpm` for pnpm commands

## Applies To

*.ps1

*.psm1

*.psd1

infra/**/*

scripts/**/*

I would recommend running something like this in the Agent Chat with Powershell command errors in context.

Review the context within chat history for examples of invalid commands used. 

Create a cursor project rule for Cursor 0.48.6 that fixes the issues of using non-windows commands. Default to PowerShell 7.5 shell style commands. 

Reference @https://docs.cursor.com/context/rules-for-ai#rules-for-ai 
to determine the correct way to implement project rules.

What should the trigger be for a Powershell MDC, please? Like auto-attatched, agent-requested.

i try ur solution ,is perfectly work !

i have this error all the time . so annoying. In my experience, cursor rules seem to be ignored by the 3rd or 4th message in a conversation

try reeky solution , i am now ok ~