如何将 Powershell 重置为默认设置?

如何将 Powershell 重置为默认设置?

我更改了许多 powershell 策略,例如运行脚本的执行策略。有没有办法让所有内容恢复到安装操作系统时的默认设置?

答案1

有一个 PowerShell 配置文件,通常C:\Users\UserA\Documents\WindowsPowerShell您可以在其中编辑/删除一些自定义条目。

当谈到“重置服务”时,这可能是可以手动完成的事情。

例如执行策略

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Undefined

此外,检查C:\Users\UserA\Documents\PowerShell\powershell.config.json您是否可以从那里实际更改执行策略

答案2

方法如下:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7#parameters

https://docs.microsoft.com/en-us/dotnet/api/microsoft.powershell.executionpolicy?view=powershellsdk-7.0.0

默认。设置默认执行策略。对于 Windows 客户端为 Restricted,对于 Windows 服务器为 RemoteSigned。

Get-ExecutionPolicy ; Get-ExecutionPolicy -List | Format-Table -AutoSize
#Cannot set execution policy. Execution policies at the MachinePolicy or UserPolicy scopes must
be set through Group Policy.
Set-Executionpolicy -Scope Process -ExecutionPolicy Default -Force
Set-Executionpolicy -Scope CurrentUser -ExecutionPolicy Default -Force
Set-Executionpolicy -Scope LocalMachine -ExecutionPolicy Default -Force
Get-ExecutionPolicy ; Get-ExecutionPolicy -List | Format-Table -AutoSize

例如:

PS C:\Users\Admin> Get-ExecutionPolicy ; Get-ExecutionPolicy -List | Format-Table -AutoSize
Restricted

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser      Restricted
 LocalMachine      Restricted


PS C:\Users\Admin> Set-Executionpolicy -Scope MachinePolicy -ExecutionPolicy Default -Force
Set-Executionpolicy : Cannot set execution policy. Execution policies at the MachinePolicy or UserPolicy scopes must
be set through Group Policy.
At line:1 char:1
+ Set-Executionpolicy -Scope MachinePolicy -ExecutionPolicy Default -Fo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Set-ExecutionPolicy], InvalidOperationException
    + FullyQualifiedErrorId : CantSetGroupPolicy,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
PS C:\Users\Admin> Set-Executionpolicy -Scope UserPolicy -ExecutionPolicy Default -Force
Set-Executionpolicy : Cannot set execution policy. Execution policies at the MachinePolicy or UserPolicy scopes must
be set through Group Policy.
At line:1 char:1
+ Set-Executionpolicy -Scope UserPolicy -ExecutionPolicy Default -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Set-ExecutionPolicy], InvalidOperationException
    + FullyQualifiedErrorId : CantSetGroupPolicy,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
PS C:\Users\Admin> Set-Executionpolicy -Scope Process -ExecutionPolicy Default -Force
PS C:\Users\Admin> Set-Executionpolicy -Scope CurrentUser -ExecutionPolicy Default -Force
PS C:\Users\Admin> Set-Executionpolicy -Scope LocalMachine -ExecutionPolicy Default -Force
PS C:\Users\Admin> Get-ExecutionPolicy ; Get-ExecutionPolicy -List | Format-Table -AutoSize
Restricted

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process      Restricted
  CurrentUser      Restricted
 LocalMachine      Restricted

相关内容