Skip to content

PowerShell

介绍

提示

  • Windows PowerShell(5.x) 是 Windows 系统自带的标准版本,仅支持 Windows 平台。

  • PowerShell 7.x 是跨平台版本,支持 Windows、macOS 和 Linux。

  • 当前记录版本为:Windows PowerShell

PowerShell 是微软开发的一种跨平台的命令行 shell 和脚本语言,旨在帮助系统管理员和开发人员自动化任务和管理系统。它基于 .NET 框架,提供了强大的功能和灵活性,使用户能够轻松地执行各种操作。

PowerShell 使用对象管道来传递数据。

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
# 列出当前目录下的所有文件和文件夹
Get-ChildItem

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

PowerShell 常用变量

powershell
# 当前 PowerShell 版本
$PSVersionTable

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

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

基于 MIT 许可发布