Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@davecan
Last active November 29, 2023 19:12
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save davecan/f3045266aa9e3441211ab55f9db70c2b to your computer and use it in GitHub Desktop.
Save davecan/f3045266aa9e3441211ab55f9db70c2b to your computer and use it in GitHub Desktop.
How to enable "Open PowerShell Here" context menu in Windows 10

After checking multiple tutorials I had to take pieces from each of the following to get this to work on my Win10 system:

Basically it uses the steps from the first article, but under the background path from the second article. Neither article on its own worked for me.

Steps:

  1. Open regedit and go to path HKEY_CLASSES_ROOT\Directory\background\shell.

  2. Create new key powershellmenu.

  3. Change the Default value of the powershellmenu key to Open in PowerShell. (or whatever)

  4. Create new key command under powershellmenu.

  5. Change the Default value of the command key to:

    C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit -Command Set-Location -LiteralPath '%L'

  6. Right-click on powershellmenu key and select Permissions, then set permissions to Full control for the Administrators group. (or whatever group/user)

Then try right-clicking in some folder and it should work.

@jtcmedia
Copy link

Change %L to %V. That's how I got it to work.

@stevland
Copy link

stevland commented Mar 9, 2018

Thanks for this. But I also had to change %L to %V for it to work, so you may want to update the instructions.

@furkankurtoglu
Copy link

Thank you so much. Also, I wonder how could we insert PowerShell icon left on it? It looks great when the programs have their icon. Thank you.

@mejdoubi
Copy link

mejdoubi commented Dec 4, 2018

Thank you so much. Also, I wonder how could we insert PowerShell icon left on it? It looks great when the programs have their icon. Thank you.

Right-click on powershellmenu key and select New > String Value, rename it to Icon and change the value to C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe

@thiemohaller
Copy link

also had to change %L to %V to get it running, cheers for the nice instructions though!

@geniusunil
Copy link

Change %L to %V and thank you so much.

@HeLiBloks
Copy link

I had to change the data in command (default) to
PowerShell -windowstyle hidden -Command "Start-Process cmd -ArgumentList '/s,/k,pushd,%V && start PowerShell && exit' -Verb RunAs"
if chocolatey is installed a lazy sysadmin could also simply run:
choco install powershellhere

@nickojs
Copy link

nickojs commented Apr 21, 2020

Thanks bro, it really helped me out!

@anton-gustafsson
Copy link

@jtcmedia Thank you!

@CiaranWelsh
Copy link

CiaranWelsh commented Aug 18, 2021

A helpful powershell script (pillaged from superuser) to accomplish the same thing:

'Directory',
'Directory\Background',
'Drive' | ForEach-Object {
  $Path = "Registry::HKEY_CLASSES_ROOT\$_\shell\PowerShellHere";
  New-Item -Path $Path -Name 'command' -Force | Out-Null;
  Set-ItemProperty -Path "$Path\command" -Name '(default)' -Value 'PowerShell -NoExit -NoLogo -Command Set-Location "%V"';
  Set-ItemProperty -Path $Path -Name '(default)' -Value 'PowerShell';
  Set-ItemProperty -Path $Path -Name 'Icon' -Value "${Env:WinDir}\System32\WindowsPowerShell\v1.0\powershell.exe,0";
}

(remember to run as admin)

@Yolakalemowa
Copy link

@CiaranWelsh

Hey there, tried that still can't see it added in context menu. Weird. Do you know how to undo the changes of that script? I'd like to start again in a VM (which I only realized should be where I test stuff like this first...) Sadly I forgot to backup regedit before this script... I'd really appreciate it

@Vikas-jk
Copy link

Vikas-jk commented Jun 3, 2022

Great thanks, if anyone needs detailed steps to add-remove powershell here, you can check here also
How to Add or Remove "Open PowerShell Here" to context menu
But I really like Powershell script, provided by CiarenWalsh
Thanks

@OneSakib
Copy link

use this as the command value this is work powershell.exe -noexit -command Set-Location -literalPath '%V'

@Ahmed-Ibrahim-Elsayed
Copy link

Ahmed-Ibrahim-Elsayed commented Jul 8, 2022

I followed the instructions above and the powershell appears in right click menu but unfortunately this error message appear when I click on it.
any help ?
Screenshot 2022-07-08 142437

@OneSakib
Copy link

OneSakib commented Jul 9, 2022

Follow this steps again, if any error come the. Reply i will help you.
https://w3code.tech/blog/open-powershell-here/

@OneSakib
Copy link

OneSakib commented Jul 9, 2022

powershell.exe -noexit -command Set-Location -literalPath '%V'

@OneSakib
Copy link

OneSakib commented Jul 9, 2022 via email

@Abiriyi
Copy link

Abiriyi commented Jul 28, 2022

I want to edit the default value of the command of powershell from registry, but I am getting "error cannot edit". See attached.
IMG_20220728_132220_364~2
IMG_20220728_132229_206~2
IMG_20220728_132211_193~2

@FCalleja
Copy link

FCalleja commented Jan 5, 2023

A helpful powershell script (pillaged from superuser) to accomplish the same thing:

'Directory',
'Directory\Background',
'Drive' | ForEach-Object {
  $Path = "Registry::HKEY_CLASSES_ROOT\$_\shell\PowerShellHere";
  New-Item -Path $Path -Name 'command' -Force | Out-Null;
  Set-ItemProperty -Path "$Path\command" -Name '(default)' -Value 'PowerShell -NoExit -NoLogo -Command Set-Location "%V"';
  Set-ItemProperty -Path $Path -Name '(default)' -Value 'PowerShell';
  Set-ItemProperty -Path $Path -Name 'Icon' -Value "${Env:WinDir}\System32\WindowsPowerShell\v1.0\powershell.exe,0";
}

(remember to run as admin)

This is great, but it's opening PowerShell 5 instead of the new PowerShell 7, can anyone guide me on what to change in the reg key so it opens ProgramFiles\PowerShell\7\pwsh.exe instead of System32\WindowsPowerShell\v1.0\powershell.exe? The only path I found in the instructions above is for the icon.

@h8rt3rmin8r
Copy link

A helpful powershell script (pillaged from superuser) to accomplish the same thing:

'Directory',
'Directory\Background',
'Drive' | ForEach-Object {
  $Path = "Registry::HKEY_CLASSES_ROOT\$_\shell\PowerShellHere";
  New-Item -Path $Path -Name 'command' -Force | Out-Null;
  Set-ItemProperty -Path "$Path\command" -Name '(default)' -Value 'PowerShell -NoExit -NoLogo -Command Set-Location "%V"';
  Set-ItemProperty -Path $Path -Name '(default)' -Value 'PowerShell';
  Set-ItemProperty -Path $Path -Name 'Icon' -Value "${Env:WinDir}\System32\WindowsPowerShell\v1.0\powershell.exe,0";
}

(remember to run as admin)

This is great, but it's opening PowerShell 5 instead of the new PowerShell 7, can anyone guide me on what to change in the reg key so it opens ProgramFiles\PowerShell\7\pwsh.exe instead of System32\WindowsPowerShell\v1.0\powershell.exe? The only path I found in the instructions above is for the icon.

I'm currently building a solution to this very thing. I'll try to remember to share when I'm done. It's crazy how much inaccurate information is out there on this topic lol

@h8rt3rmin8r
Copy link

Just tested and the following script is working flawlessly for setting up right-click and SHIFT + right-click menus on both directories and attached storage devices for PowerShell 7:

#!/usr/bin/env pwsh
#------------------------------------------------------------------------------
#
# [ regedit-open-ps-here.ps1 ]
#
# Description:
#
#    A PowerShell script that will loop over the following registry entries:
#
#    HKEY_CLASSES_ROOT\Directory\ (right-click menu on directories)
#    HKEY_CLASSES_ROOT\Directory\Background (SHIFT + right-click menu on directories)
#    HKEY_CLASSES_ROOT\Drive (right-click menu on attached storage devices)
#
#    ... and create new items that add an option to "Open PowerShell 7 Here"
#
#    NOTE: This script must be run as "Administrator" (because we're editing
#    the Windows Registry)
#
#    NOTE: This script sets up a right-click menu option for PowerShell 7, not
#    the older built-in version of PowerShell!
#
# Reference:
#
#    # Github Gist by davecan (and related comments)
#    http://bit.ly/3GYmlbO
#
#    # My comment on the above-referenced Github Gist
#    http://bit.ly/3H3zu3n
#
#    # How to Check If a PowerShell Script Is Run As Administrator
#    http://bit.ly/3R8PRAq
#
#------------------------------------------------------------------------------

Write-Host "Checking for elevated permissions..."

if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
	
    Write-Warning "Insufficient permissions to run this script."
	Write-Host "Open the PowerShell console as an administrator and run this script again."
	
    Break

} else {
	Write-Host "Success: Code is running as administrator." -ForegroundColor Green
    Write-Host "Beginning edits to the Windows Registry ..."
    
    'Directory',
    'Directory\Background',
    'Drive' | ForEach-Object {
      $Path = "Registry::HKEY_CLASSES_ROOT\$_\shell\PowerShellMenu";
      New-Item -Path $Path -Name 'command' -Force | Out-Null;
      Set-ItemProperty -Path "$Path\command" -Name '(default)' -Value '"C:\Program Files\PowerShell\7\pwsh.exe" -NoExit -Command Set-Location -LiteralPath "%V"';
      Set-ItemProperty -Path $Path -Name '(default)' -Value 'Open PowerShell 7 Here';
      Set-ItemProperty -Path $Path -Name 'Icon' -Value "C:\Program Files\PowerShell\7\pwsh.exe,0";
    }
    
    Write-Host "Operations complete"
}

@FCalleja - I hope this helps ;-)

@Weldryn
Copy link

Weldryn commented Feb 21, 2023

A helpful powershell script (pillaged from superuser) to accomplish the same thing:

'Directory',
'Directory\Background',
'Drive' | ForEach-Object {
  $Path = "Registry::HKEY_CLASSES_ROOT\$_\shell\PowerShellHere";
  New-Item -Path $Path -Name 'command' -Force | Out-Null;
  Set-ItemProperty -Path "$Path\command" -Name '(default)' -Value 'PowerShell -NoExit -NoLogo -Command Set-Location "%V"';
  Set-ItemProperty -Path $Path -Name '(default)' -Value 'PowerShell';
  Set-ItemProperty -Path $Path -Name 'Icon' -Value "${Env:WinDir}\System32\WindowsPowerShell\v1.0\powershell.exe,0";
}

(remember to run as admin)

This is great, but it's opening PowerShell 5 instead of the new PowerShell 7, can anyone guide me on what to change in the reg key so it opens ProgramFiles\PowerShell\7\pwsh.exe instead of System32\WindowsPowerShell\v1.0\powershell.exe? The only path I found in the instructions above is for the icon.

Use pwsh instead of ${Env:WinDir}\System32\WindowsPowerShell\v1.0\powershell.exe and Powershell 7 will be launched instead. Works for me

@tech-bash
Copy link

Mine also worked with '%V'.

@d3NNEb
Copy link

d3NNEb commented Mar 22, 2023

shift + right-click on folder works

@Lunar808
Copy link

@davecan You saved me man
I did create in the past this key, but it didn't work and I pretty much gave up(thanks microsoft)
but my mistake was that I created this key under /shell/ and not /Background/shell

Thanks man

@iubaidrmn
Copy link

I followed this.
https://www.youtube.com/watch?v=RzNiD4WQQ1M

solved my issue

@LtqxWYEG
Copy link

LtqxWYEG commented Jul 8, 2023

I found this page via Google. This is one of the first search results.
So... I wanted to start PS as administrator instead. I tried for an hour and ended up with this:

PowerShell -Command "Start-Process powershell -Verb runAs -ArgumentList '-NoExit', '-Command', 'Set-Location -LiteralPath \"%V\"'"

Here's a registry file for everyone. It adds the context entry everywhere. It adds the icon as well.
(Originally from WinaeroTweaker - but I didn't like that powershell was running "inside" CMD.exe)

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\OpenElevatedPS]
@="Open PowerShell here as administrator"
"Icon"="powershell.exe"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\shell\OpenElevatedPS\command]
@="PowerShell -Command \"Start-Process powershell -Verb runAs -ArgumentList '-NoExit', '-Command', 'Set-Location -LiteralPath \\\"%V\\\"'\""

[HKEY_CLASSES_ROOT\Directory\Background\shell\OpenElevatedPS]
@="Open PowerShell here as administrator"
"Icon"="powershell.exe"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\OpenElevatedPS\command]
@="PowerShell -Command \"Start-Process powershell -Verb runAs -ArgumentList '-NoExit', '-Command', 'Set-Location -LiteralPath \\\"%V\\\"'\""

[HKEY_CLASSES_ROOT\Drive\shell\OpenElevatedPS]
@="Open PowerShell here as administrator"
"Icon"="powershell.exe"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Drive\shell\OpenElevatedPS\command]
@="PowerShell -Command \"Start-Process powershell -Verb runAs -ArgumentList '-NoExit', '-Command', 'Set-Location -LiteralPath \\\"%V\\\"'\""

[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\OpenElevatedPS]
@="Open PowerShell here as administrator"
"Icon"="powershell.exe"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\OpenElevatedPS\command]
@="PowerShell -Command \"Start-Process powershell -Verb runAs -ArgumentList '-NoExit', '-Command', 'Set-Location -LiteralPath \\\"%V\\\"'\""

And here is the version using Windows Terminal:

PowerShell -Command "Start-Process -Verb RunAs wt.exe \"powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'\""
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\OpenElevatedPS]
@="Open PowerShell here as administrator"
"Icon"="powershell.exe"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\shell\OpenElevatedPS\command]
@="PowerShell -Command \"Start-Process -Verb RunAs wt.exe \\\"powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'\\\"\""

[HKEY_CLASSES_ROOT\Directory\Background\shell\OpenElevatedPS]
@="Open PowerShell here as administrator"
"Icon"="powershell.exe"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\OpenElevatedPS\command]
@="PowerShell -Command \"Start-Process -Verb RunAs wt.exe \\\"powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'\\\"\""

[HKEY_CLASSES_ROOT\Drive\shell\OpenElevatedPS]
@="Open PowerShell here as administrator"
"Icon"="powershell.exe"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Drive\shell\OpenElevatedPS\command]
@="PowerShell -Command \"Start-Process -Verb RunAs wt.exe \\\"powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'\\\"\""

[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\OpenElevatedPS]
@="Open PowerShell here as administrator"
"Icon"="powershell.exe"
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\LibraryFolder\background\shell\OpenElevatedPS\command]
@="PowerShell -Command \"Start-Process -Verb RunAs wt.exe \\\"powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'\\\"\""

@SingusTheTexan
Copy link

i changed it to "%v" and it does not work.

@LtqxWYEG
Copy link

LtqxWYEG commented Nov 17, 2023

Don't know what to tell you. Update your software, maybe? Or did you forget the "/", slashs?
I'm using windows 10 pro. Newest everything, afaik. Shouldn't have any effect on this, though.

@SingusTheTexan
Copy link

ok i do have to update then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment