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.
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
NEVER use PowerShell commands in terminal suggestions.
Always use Windows CMD commands (batch commands) for all operations.
Use standard Windows CMD syntax with commands like “dir”, “type”, “copy”, “move”, “del”, etc.
For package installation, use npm, yarn, or other package managers with their CMD-compatible syntax.
Avoid commands that begin with “ps”, “pwsh”, or contain PowerShell syntax like “$_”, “-ErrorAction”, etc.
Use Windows environment variables with %VARIABLE% syntax instead of PowerShell’s $env:VARIABLE.
For file path references, use Windows-style paths with backslashes (C:\path\to\file).
When suggesting command chaining, use “&” or “&&” instead of PowerShell’s pipe operator.
For conditional operations, use “if” with CMD syntax instead of PowerShell conditionals.
Use “for” loops with CMD syntax rather than PowerShell’s foreach.
Use “findstr” instead of PowerShell’s Select-String for text searching.
For redirecting output, use “>” and “>>” operators rather than PowerShell’s Out-File.
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.
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
Verify PowerShell Installations
Confirmed Windows PowerShell 5.1 at: C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
Disabled “Use preview box for terminal Ctrl+K” in Cursor settings
This setting affects how responses are streamed to the shell
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
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.