skip to Main Content

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)?

2

Answers


  1. You simply need to change the exe path to vs code in the following registry key

    ComputerHKEY_CLASSES_ROOTSystemFileAssociations.ps1ShellEditCommand
    

    Double click the default property and replace the ISE path with the path to your vscode.exe

    Login or Signup to reply.
  2. 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.CoreRegistry::HKEY_CLASSES_ROOTSystemFileAssociations.ps1ShellEditCommand'
    
    # 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`""
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search