Skip to content

PowerShell

提示

当前记录版本 PowerShell 5.x

PowerShell 设置

更新脚本执行策略

提示

以管理员身份打开 PowerShell

  • RemoteSigned:允许本地脚本无签名运行,但要求从互联网下载的脚本必须有数字签名。
  • Unrestricted:允许所有脚本运行,适合测试环境,但安全性较低。
powershell
Set-ExecutionPolicy RemoteSigned

查看当前策略:

powershell
 Get-ExecutionPolicy

PowerShell 配置文件

创建 powershell 配置文件(如果不存在):

powershell
if (-not (Test-Path $profile)) { New-Item $profile -Force }

查看当前配置文件路径:

powershell
$profile
常见路径
  • Powershell 5%userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  • Powershell 6+%userprofile%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

打开配置文件:

powershell
notepad $profile

重新加载配置文件:

powershell
. $profile

配置代理

如果需要永久设置,则写入 PowerShell 配置文件即可

powershell
$env:HTTP_PROXY = "http://127.0.0.1:7897"
$env:HTTPS_PROXY = "http://127.0.0.1:7897"
$env:NO_PROXY = "localhost,127.0.0.1,::1"

验证:

powershell
# 查看所有环境变量
Get-ChildItem Env:
gci Env:

# 查看某个
echo $env:HTTP_PROXY
echo $env:HTTPS_PROXY
echo $env:NO_PROXY

PowerShell 常用变量

powershell
# 当前用户的 PowerShell 配置文件路径
$profile

# 当前用户的主目录路径
$home

PowerShell 常用命令

powershell
# 显示当前日期和时间
Get-Date

基于 MIT 许可发布