powershell 用于添加上下文菜单项以打开PowerShell命令提示符并使用PowerShell ISE编辑文件的功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 用于添加上下文菜单项以打开PowerShell命令提示符并使用PowerShell ISE编辑文件的功能相关的知识,希望对你有一定的参考价值。

function Add-PowerShellContextMenu{
    [CmdletBinding()]
    param(
        [Parameter(Position=0)]
        [ValidateSet('openPowerShellHere','editWithPowerShellISE')]
        $contextType,
        $platform='x64',
        [switch]$noProfile,
        [switch]$asAdmin
    )
    $versionToOpen = 'PowerShell (x64)'
    $powerShellExe = 'powershell.exe'
    if ($contextType -eq 'editWithPowerShellISE') { 
        $powerShellExe = 'PowerShell_ISE.exe' 
         $versionToOpen = 'PowerShell ISE (x64)'
    }
    $powerShellPath = "$env:WINDIR\sysWOW64\WindowsPowerShell\v1.0\$powershellExe"
    if ($platform -eq 'x86'){ 
        $powerShellPath = "$env:WINDIR\sysWOW64\WindowsPowerShell\v1.0\$powershellExe" 
        $versionToOpen = $versionToOpen -replace 'x64','x86'
    }
    if ($contextType -eq 'openPowerShellHere'){
        $menu = "Open Windows $versionToOpen here"
        $command = "$powerShellPath -NoExit -Command ""Set-Location '%V'"""
        if ($noProfile.IsPresent){
            $command = $command -replace 'NoExit', 'NoExit -noProfile'
        }
        if ($asAdmin.IsPresent){
            $menu += ' as Administrator'
            'directory', 'directory\background', 'drive' | foreach {
                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 ''
            }
        }
        else{
            'directory', 'directory\background', 'drive' | foreach {
                New-Item -Path "Registry::HKEY_CLASSES_ROOT\$_\shell" -Name $menu -Value $menu -Force | Out-Null
                New-Item -Path "Registry::HKEY_CLASSES_ROOT\$_\shell\$menu\command" -Value $command | Out-Null
            }
        }
    }
    elseif($contextType -eq 'editWithPowerShellISE'){
        $menu = "Edit with $versionToOpen"
        $command = $powerShellPath
        if ($noProfile.IsPresent){
            $command += ' -noProfile'
        }
        if($asAdmin.IsPresent){
            $menu += ' as Administrator'
            dir "Registry::HKEY_CLASSES_ROOT" | where PSChildName -like 'Microsoft.PowerShell*' | foreach{
                if (!(Test-Path "Registry::$($_.Name)\shell")){
                    New-Item "Registry::$($_.Name)\shell" | Out-Null
                }
                New-Item "Registry::$($_.Name)\shell\" -Name runas\command -Force |
                Set-ItemProperty -Name '(default)' -Value "$command ""%1""" -PassThru |
                Set-ItemProperty -Path {$_.PSParentPath} -Name '(default)' -Value $menu -PassThru |
                Set-ItemProperty -Name HasLUAShield -Value ''
            }
        }
        else{
            dir "Registry::HKEY_CLASSES_ROOT" | where PSChildName -like 'Microsoft.PowerShell*' | foreach{
                if (!(Test-Path "Registry::$($_.Name)\shell")){
                    New-Item "Registry::$($_.Name)\shell" | Out-Null
                }
                New-Item "Registry::$($_.Name)\shell\$menu\command" -Value "$command ""%1""" -Force | Out-Null  
            }
        }
        
    }
    
}

以上是关于powershell 用于添加上下文菜单项以打开PowerShell命令提示符并使用PowerShell ISE编辑文件的功能的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Grub 中添加菜单项以在命令行中启动 Ubuntu [关闭]

Outlook VSTO附件在邮件主题中选择了文本和上下文菜单

哪些注册表命令允许我从包含撇号的文件夹的右键单击上下文菜单中以管理员身份运行 PowerShell 5 / CMD?

如何在 Windows 资源管理器中添加“在此处打开 git-bash ...”上下文菜单?

共享点列表字段的上下文菜单

怎么打开windows powershell