How Powershell becomes more pleasant for the agent

The entire terminal intigration is dirty and should be cleaned. This was probably taken 1:1 from VScode, but that does not work for the IDE for cursor, makes the agent partially blind and causes race conditions. Powershell without customization is an imposition. Use Powershell 7.5 and remove the content from shellIntegration.ps1 and replace it with

# ---------------------------------------------------------------------------------------------
#   Copyright (c) Microsoft Corporation. All rights reserved.
#   Licensed under the MIT License. See License.txt in the project root for license information.
# ---------------------------------------------------------------------------------------------

# Deactivates the VS Code Shell integration in PowerShell
$Global:__VSCodeOriginalPrompt = $function:Prompt

# Defines a cleaned prompt function without escape sequences
function Global:Prompt() {
    return $Global:__VSCodeOriginalPrompt.Invoke()
}

# Prevents PSReadLine overwriting and removes VS code-specific handlers
if (Get-Module -Name PSReadLine) {
    # Resetting the PSConsoleHostReadLine function
    if ($null -ne $__VSCodeOriginalPSConsoleHostReadLine) {
        $function:PSConsoleHostReadLine = $__VSCodeOriginalPSConsoleHostReadLine
    }
    # Remove all VS Code keybindings
    Remove-PSReadLineKeyHandler -Chord 'F12,e' -ErrorAction SilentlyContinue
    Remove-PSReadLineKeyHandler -Chord 'F12,f' -ErrorAction SilentlyContinue
}

Write-Host "Shell integration has been deactivated."

# Cleanup of all VS Code-specific environment variables.
# Does not completely remove VSCode, partially resets it.
$env:VSCODE_NONCE = $null
$env:VSCODE_STABLE = $null
$env:VSCODE_ENV_REPLACE = $null
$env:VSCODE_ENV_PREPEND = $null
$env:VSCODE_ENV_APPEND = $null

# Native PowerShell autocompletion without OSC-633 sequences
function Send-Completions {
    $commandLine = ""
    $cursorIndex = 0
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$commandLine, [ref]$cursorIndex)
    
    # Use standard TabExpansion2 for completions
    try {
        $completions = TabExpansion2 -inputScript $commandLine -cursorColumn $cursorIndex -ErrorAction Stop
        if ($completions -and $completions.CompletionMatches) {
            $completions.CompletionMatches | ForEach-Object {
                Write-Output $_.CompletionText
            }
        }
    } catch {
        Write-Error "Error during completion: $_"
    }
}

# Final confirmation
Write-Host "Terminal is now AI-compatible. No VS Code specific functions active."

It’s not perfect but works much better.
Caution! Powershell is very powerful and is loaded in the user context of the user and their Powershell profile.
You should actually restrict it, for example with

  • Set PowerShell Execution Policy
  • Activate PowerShell Constrained Language Mode
  • Restrict PowerShell profiles
  • Command whitelisting (shell wrapper) or via Cursor Settings

or you choose a secure shell like the cmd

Instructions: Stable setup of PowerShell & Cursor IDE


1. Deactivate VS code shell integration

  • Customize script: Remove all lines with $([char]0x1b)]633;... from shellIntegration.ps1.
  • Clean up environment variables**:
    $env:VSCODE_NONCE = $null  
    $env:VSCODE_STABLE = $null  
    $env:VSCODE_ENV_REPLACE = $null # Analog for all VSCODE_* variables
    

2. Update PSReadLine

# Uninstall old version (admin rights required)
Uninstall-Module PSReadLine -Force  

# Install latest version
Install-Module PSReadLine -AllowPrerelease -Force -Scope AllUsers  

# Load module manually (if not automatically)
Import modules PSReadLine  

3. Optimize PSReadLine configuration

  • Open profile:
    notepad $PROFILE  
    
  • Add the following lines:
    Set-PSReadLineOption -PredictionSource None  
    Set-PSReadLineOption -BellStyle None  
    

4. Correct terminal buffer

# Execute in the cursor IDE
$Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(120, 1000)  

5. Tips for avoiding errors
  • Load PSReadLine manually:
    Import modules PSReadLine  
    
  • Update PSReadLine:
    Update modules PSReadLine  
    
  • Check command syntax**: Always ensure closing brackets/quotes.
  • Alternatives to Write-Host:
    “number $i” # Instead of Write-Host ”number $i”  
    

Summary of the main problems

  1. Incompatible escape sequences:
    VS Code specific codes (OSC 633) resulted in "ghost text ”.
    2 PSReadLine conflicts:
    Outdated versions or incorrect configuration caused crashes.
  2. Terminal buffer errors:
    Incorrect buffer size (BufferHeight: 1) interfered with the display.
  3. Syntax error:
    Missing brackets/quotes triggered unexpected errors.

Done! :rocket: The cursor IDE should now work stable with PowerShell.

3 Likes

Awesome. But might these customizations cause issues if the Cursor team decides to make Cursor more powershell friendly in the future?

I can’t rule that out.
I also assume that the developers will change something in the future, a few adjustments have already been made, which I see very positively, but now has no influence on mine.
The shellIntegration.ps1 is located in the path

c:\Users\<your_user>\AppData\Local\Programs\cursor\resources\app\out\vs\workbench\contrib\terminal\common\scripts\

Create a backup of this before editing and overwrite if necessary.