PS: Add OpenAdminPS To Right Click Menu In Windows

7 minute read

Description:

So I had a previous post where I talked about adding a line to my setup of QuickCliq to run a batch to run PS as admin and it works great, still use all the time. I started digging and found a way you can add it to right click menu as well in Windows itself.

NOTE: I still highly recommend using my QuickCliq setup to run batch files as it has shortcut commands and you can add/take away real easily without playing around in the registry. I tried to refactor the script below as this (see below), and it doesn’t work – it moves the “Open PS here  (Admin) to the main menu and just looks tacky. I’m usually good about refactoring scripts, but I screwed this one up somehow?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
   $Paths = @()
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\shell\01MenuPowerShell"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\open"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\open\command"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas\command"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\shell\Powershell"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\background\shell\Powershell"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell\open"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell\open\command"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\background\shell\runas"
   $Paths += "HKLM:\SOFTWARE\Classes\Directory\background\shell\runas\command"

   ForEach ($Path in $Paths)
   {
      If ((Test-Path $Path) -ne $True)
      {
         New-Item $Path -Force -ErrorAction SilentlyContinue | Out-Null
      }
   }

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\shell\01MenuPowerShell"
   $Params.Name = "MUIVerb"
   $Params.Value = "Open PowerShell"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\shell\01MenuPowerShell"
   $Params.Name = "Icon"
   $Params.Value = "powershell.exe"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\shell\01MenuPowerShell"
   $Params.Name = "ExtendedSubCommandsKey"
   $Params.Value = "Directory\\ContextMenus\\MenuPowerShell"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell"
   $Params.Name = "MUIVerb"
   $Params.Value = "Open PowerShell"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell"
   $Params.Name = "Icon"
   $Params.Value = "powershell.exe"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell"
   $Params.Name = "ExtendedSubCommandsKey"
   $Params.Value = "Directory\\ContextMenus\\MenuPowerShell"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell\open"
   $Params.Name = "MUIVerb"
   $Params.Value = "PowerShell here"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell\open"
   $Params.Name = "Icon"
   $Params.Value = "powershell.exe"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell\open\command"
   $Params.Name = "(default)"
   $Params.Value = "powershell.exe -noexit -command Set-Location '%V'"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\runas"
   $Params.Name = "MUIVerb"
   $Params.Value = "PowerShell here (Admin)"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\runas"
   $Params.Name = "Icon"
   $Params.Value = "powershell.exe"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\runas"
   $Params.Name = "HasLUAShield"
   $Params.Value = ""
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\runas\command"
   $Params.Name = "(default)"
   $Params.Value = "powershell.exe -noexit -command Set-Location '%V'"
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\shell\Powershell"
   $Params.Name = "Extended"
   $Params.Value = ""
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force -ErrorAction SilentlyContinue | Out-Null
   Clear-Variable -Name Params

   $Params = @{}
   $Params.Path = "HKLM:\SOFTWARE\Classes\Directory\background\shell\Powershell"
   $Params.Name = "Extended"
   $Params.Value = ""
   $Params.PropertyType = "String"
   New-ItemProperty @Params -Force -ErrorAction SilentlyContinue | Out-Null
   Clear-Variable -Name Params

To Resolve:

  1. Update 2018-04-04: If you just want to open an Administrator PS Prompt in your current directory, run:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    $menu = 'OpenPSHere'
    $command = "$PSHOME\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'"""
    
    'directory', 'directory\background', 'drive' | ForEach-Object {
       New-Item -Path "Registry::HKEY_CLASSES_ROOT\$_\shell" -Name runas\command -Force |
       Set-ItemProperty -Name '(default)' -Value $command -PassThru |
       Set-ItemProperty -Path {$_.PSParentPath} -Name '(default)' -Value $menu -PassThru |
       Set-ItemProperty -Name HasLUAShield -Value ''
       }
    
  2. Here is a version that adds “Open Powershell” with a submenu of “Open Powershell here” and “Open Powershell here Admin”

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    if((Test-Path "HKLM:\SOFTWARE\Classes\Directory\shell\01MenuPowerShell") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\Directory\shell\01MenuPowerShell" -force -ea SilentlyContinue };
    if((Test-Path "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell" -force -ea SilentlyContinue };
    if((Test-Path "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\open") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\open" -force -ea SilentlyContinue };
    if((Test-Path "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\open\command") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\open\command" -force -ea SilentlyContinue };
    if((Test-Path "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas" -force -ea SilentlyContinue };
    if((Test-Path "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas\command") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas\command" -force -ea SilentlyContinue };
    if((Test-Path "HKLM:\SOFTWARE\Classes\Directory\shell\Powershell") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\Directory\shell\Powershell" -force -ea SilentlyContinue };
    if((Test-Path "HKLM:\SOFTWARE\Classes\Directory\background\shell\Powershell") -ne $true) {  New-Item "HKLM:\SOFTWARE\Classes\Directory\background\shell\Powershell" -force -ea SilentlyContinue };
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\shell\01MenuPowerShell' -Name 'MUIVerb' -Value "Open PowerShell" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\shell\01MenuPowerShell' -Name 'Icon' -Value "powershell.exe" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\shell\01MenuPowerShell' -Name 'ExtendedSubCommandsKey' -Value "Directory\\ContextMenus\\MenuPowerShell" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell' -Name 'MUIVerb' -Value "Open PowerShell" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell' -Name 'Icon' -Value "powershell.exe" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\background\shell\01MenuPowerShell' -Name 'ExtendedSubCommandsKey' -Value "Directory\\ContextMenus\\MenuPowerShell" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\open' -Name 'MUIVerb' -Value "PowerShell here" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\open' -Name 'Icon' -Value "powershell.exe" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\open\command' -Name '(default)' -Value "powershell.exe -noexit -command Set-Location '%V'" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas' -Name 'MUIVerb' -Value "PowerShell here (Admin)" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas' -Name 'Icon' -Value "powershell.exe" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas' -Name 'HasLUAShield' -Value "" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\ContextMenus\MenuPowerShell\shell\runas\command' -Name '(default)' -Value "powershell.exe -noexit -command Set-Location '%V'" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\shell\Powershell' -Name 'Extended' -Value "" -PropertyType String -Force -ea SilentlyContinue;
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Directory\background\shell\Powershell' -Name 'Extended' -Value "" -PropertyType String -Force -ea SilentlyContinue;
    
  3. The following script will add “AdminPS”, “AdminPowershellISE”, and “OpenPowershellHere” to your right click menu for any time you are in the file system (Windows Explorer/Desktop):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    
    If (!(Test-Path "HKCR:"))
    {
    New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
    }
    $registryPath = "HKCR:\Directory\Background\shell"
    $Name = "AdminPowershell"
    New-Item -Path $registryPath -Name $Name -Force | Out-Null
    $registryPath = "HKCR:\Directory\Background\shell\AdminPowershell"
    $Name = "command"
    New-Item -Path $registryPath -Name $Name -Force | Out-Null
    $registryPath = "HKCR:\Directory\Background\shell\AdminPowershell\command"
    $Name = "(Default)"
    $value = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe start-process powershell -verb runas"
    IF(!(Test-Path $registryPath))
    {
    New-Item -Path $registryPath -Force | Out-Null
    }
    New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
    If (!(Test-Path "HKCR:"))
    {
    New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
    }
    $registryPath = "HKCR:\Directory\Background\shell"
    $Name = "AdminPowershellISE"
    New-Item -Path $registryPath -Name $Name -Force | Out-Null
    $registryPath = "HKCR:\Directory\Background\shell\AdminPowershellISE"
    $Name = "command"
    New-Item -Path $registryPath -Name $Name -Force | Out-Null
    $registryPath = "HKCR:\Directory\Background\shell\AdminPowershellISE\command"
    $Name = "(Default)"
    $value = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe start-process powershell_ise -verb runas"
    IF(!(Test-Path $registryPath))
    {
    New-Item -Path $registryPath -Force | Out-Null
    }
    New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
    If (!(Test-Path "HKCR:"))
    {
    New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
    }
    $registryPath = "HKCR:\Directory\Background\shell"
    $Name = "\OpenPowershellHere"
    New-Item -Path $registryPath -Name $Name -Force | Out-Null
    $registryPath = "HKCR:\Directory\Background\shell\OpenPowershellHere"
    $Name = "command"
    New-Item -Path $registryPath -Name $Name -Force | Out-Null
    $registryPath = "HKCR:\Directory\Background\shell\\OpenPowershellHere\command"
    $Name = "(Default)"
    $value = "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command Set-Location -LiteralPath '%V'"
    IF(!(Test-Path $registryPath))
    {
    New-Item -Path $registryPath -Force | Out-Null
    }
    New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
    

Comments