方法由 StackOverflow 上的 @JPBlanc 网友提供。只需要在 PowerShell 中执行一次以下代码,以后即可双击运行 .ps1 脚本。优点是可以开可以关。原文是法文,已由 @Andy Arismendi 网友翻译为英文。 function Set-PowAsDefault { [CmdletBinding()] Param ( [Parameter(mandatory=$false,ValueFromPipeline=$false)] [Alias("Active")] [switch] [bool]$On ) begin { if ($On.IsPresent) { Write-Host "Call Powershell for .PS1 files." } else { Write-Host "Try to go back." } } Process { # Text Menu [string]$TexteMenu = "Go inside PowerShell" # Text of the program to create [string] $TexteCommande = "%systemroot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command ""&'%1'""" # Key to create [String] $clefAModifier = "HKLM:\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Open\Command" try { $oldCmdKey = $null $oldCmdKey = Get-Item $clefAModifier -ErrorAction SilentlyContinue $oldCmdValue = $oldCmdKey.getvalue("") if ($oldCmdValue -ne $null) { if ($On.IsPresent) { $slxOldValue = $null $slxOldValue = Get-ItemProperty $clefAModifier -Name "slxOldValue" -ErrorAction SilentlyContinue if ($slxOldValue -eq $null) { New-ItemProperty $clefAModifier -Name "slxOldValue" -Value $oldCmdValue -PropertyType "String" | Out-Null New-ItemProperty $clefAModifier -Name "(default)" -Value $TexteCommande -PropertyType "ExpandString" | Out-Null Write-Host "Done !" } else { Write-Host "Already done !" } } else { $slxOldValue = $null $slxOldValue = Get-ItemProperty $clefAModifier -Name "slxOldValue" -ErrorAction SilentlyContinue if ($slxOldValue -ne $null) { New-ItemProperty $clefAModifier -Name "(default)" -Value $slxOldValue."slxOldValue" -PropertyType "String" | Out-Null Remove-ItemProperty $clefAModifier -Name "slxOldValue" Write-Host "Done !" } else { Write-Host "No former value !" } } } } catch { $_.exception.message } } end {} } 用方法很简单,Set-PowAsDefault -On为打开,Set-PowAsDefault为关闭。需要管理员权限执行。