Search code examples
powershellvisual-studio-coderegistrypowershell-ise

Change the default "Edit" action for .ps1 from ISE to VS Code


Like the person in this question, it is frustrating that such obvious things have no clear answers (I've looked at 20 pages and found nothing yet).

The default right-click "Edit" action in Windows for .ps1 files is to open with PowerShell_ISE. ISE is an ancient and bloated relic of a bygone age, and incredibly slow to open, so when you accidently do right-click > Edit, it is tortuous watching ISE slowly open.

How can we programmatically alter the default "Edit" action for .ps1 / .psm1 etc such that they will point to VS Code instead of ISE?

Alternatively, if we can't alter it, can we completely remove it (so that "Edit with VS Code" is the only option left)?


Solution

  • This is actually pretty easy to do in PowerShell, or by tweaking the registry by hand.

    Here's a script that will change things over.

    Because you're going to be changing HKEY_CLASSES_ROOT, you're going to need to be running as administrator:

    # This a "provider qualified" path to the edit command
    $editRegistryPath = 'Microsoft.PowerShell.Core\Registry::HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\Edit\Command'
    
    # This is how we will get the default value
    $defaultEdit = Get-ItemProperty $editRegistryPath  -Name '(default)'
    
    # If we couldn't get it, something's wrong, so return
    if (-not $defaultEdit) {
        return
    }
    
    # Get the path to code.cmd (this must be in $env:PATH)
    $codePath = Get-Command code -CommandType Application | Select-Object -First 1 | Select-Object -ExpandProperty Source
    
    # Change the property.  Make sure we quote our command name and our arguments.
    Set-ItemProperty -Path $editRegistryPath -Name '(default)' -Value "`"$($codePath)`" `"%1`""