当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 配置文件:入门指南

作者:精品下载站 日期:2024-12-14 13:11:41 浏览:16 分类:玩电脑

PowerShell 配置文件:入门指南


您是否曾经对重复运行确切的命令或脚本感到厌倦?为什么不让 PowerShell 配置文件让您的生活更轻松呢?

PowerShell 配置文件是由书面指令组成的脚本,在您启动 PowerShell 会话时执行。在本教程中,您将学习多种配置 PowerShell 配置文件的方法。

听起来不错?请继续关注并自动执行自定义代码!

先决条件

  • Windows 或 Linux 计算机 - 本教程使用 Windows 11 PC。
  • 您的系统上安装了 PowerShell 7 或 5.1。

创建 PowerShell 配置文件

PowerShell 配置文件是每次启动 PowerShell 会话时运行的常规脚本。根据操作系统的不同,“当前用户、当前主机”的 PowerShell 配置文件位于以下位置:

Operating System

PowerShell 配置文件位置

Windows

$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

macOS and Linux

~/.config/powershell/Microsoft.Powershell_profile.ps1

要创建 PowerShell 配置文件:

1. 以管理员身份打开 PowerShell,然后运行以下 Test-Path 命令检查 PowerShell 配置文件是否已存在。

 Test-Path $profile

在下面,您可以看到命令返回 False,这确认尚不存在现有的 PowerShell 配置文件。

[玩转系统] PowerShell 配置文件:入门指南

事实上,当前用户和所有用户都有不同的配置文件,如下所示。但是,当谈论 Windows PowerShell 配置文件时,您指的是“当前用户 - 当前主机”配置文件。

Windows PowerShell 5.1

Profile

路径

Current User - Current Host

$Home\[我的]Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Current User - All Hosts

$Home\[我的]Documents\WindowsPowerShell\Profile.ps1

All Users - Current Host

$PSHOME\Microsoft.PowerShell_profile.ps1

All Users - All Hosts

$PSHOME\Profile.ps1

? $Home 变量引用当前用户的主目录,而 $PSHOME 变量引用 PowerShell 的安装路径。

PowerShell 7.x

Profile

路径

Current User - Current Host – Windows

$Home\[我的]Documents\Powershell\Microsoft.Powershell_profile.ps1

Current User - Current Host – Linux/macOS

~/.config/powershell/Microsoft.Powershell_profile.ps1

Current User - All Hosts – Windows

$Home\[我的]Documents\Powershell\Profile.ps1

Current User - All Hosts – Linux/macOS

~/.config/powershell/profile.ps1

All Users - Current Host – Windows

$PSHOME\Microsoft.Powershell_profile.ps1

All Users - Current Host – Linux/macOS

/usr/local/microsoft/powershell/7/Microsoft.Powershell_profile.ps1

All Users - All Hosts – Windows

$PSHOME\Profile.ps1

All Users - All Hosts – Linux/macOS

/usr/local/microsoft/powershell/7/profile.ps1

2. 接下来,运行以下 New-Item 命令来创建您的 PowerShell 配置文件。此命令在当前用户 - 当前主机配置文件路径 ($profile) 中创建一个脚本文件。

New-Item -path $profile -type file -force

[玩转系统] PowerShell 配置文件:入门指南

现在,运行以下每个命令来检查每个用户的 PowerShell 配置文件的路径。

# Return the PowerShell profile path of the Current user - Current host
$profile
# Return the PowerShell profile path of the Current user - All hosts
$profile.CurrentUserAllHosts
# Return the PowerShell profile path of the All users - Current host
$profile.AllUsersCurrentHost
# Return the PowerShell profile path of the All users - Current host
$profile.AllUsersAllHosts

下面的输出显示了 PowerShell 5 和 7 的每个配置文件的路径。

[玩转系统] PowerShell 配置文件:入门指南

[玩转系统] PowerShell 配置文件:入门指南

自定义 PowerShell 配置文件

新创建的 PowerShell 配置文件默认为空。您可以在 PowerShell 配置文件中包含经常使用的变量、别名和命令,而无需在每个会话中重新创建它们。

但首先,您可能需要更改 PowerShell 的执行策略,因为 PowerShell 默认情况下禁用脚本执行,以防止恶意脚本影响系统。

运行以下命令将 PowerShell 的执行策略 (Set-ExecutionPolicy) 设置为 RemoteSigned。

RemoteSigned 策略规定,任何脚本如果不是在运行脚本的系统上创建的,都应该进行签名。此策略要求来自 Internet 或由 Internet Explorer、Outlook 或 Messenger 等应用程序下载的脚本具有数字签名。

Set-ExecutionPolicy RemoteSigned

[玩转系统] PowerShell 配置文件:入门指南

现在,运行以下任一命令,该命令不提供输出,但在 PowerShell ISE 或记事本中打开您的 PowerShell 配置文件 ($PROFILE)。

# Opens PowerShell profile in PowerShell ISE
ise $PROFILE 
# Opens PowerShell profile in Notepad
notepad $PROFILE

设置别名以实现快速命令和功能的辅助功能

别名是最常见的自定义,因为它们允许您通过所选名称引用 PowerShell 命令。

将以下别名添加到您的 PowerShell 配置文件中,保存更改但不要关闭文件。

# Create aliases for frequently used applications.
New-Alias np Notepad.exe
     
# Create aliases for frequently used commands
Set-Alias tn Test-NetConnection

自定义 PowerShell 控制台的外观

除了别名之外,您还可以更改 PowerShell 的运行方式并通过个性化提示增强其交互体验。

1. 在 PowerShell 配置文件中包含以下内容以反映默认设置中的更改,保存更改并关闭文件。

下面的 Color-Console 函数通过更改 PowerShell 的颜色并显示当前日期/时间来更改 PowerShell 的默认外观。

# Create a function to change colors in PowerShell
function Color-Console {
	$Host.ui.rawui.backgroundcolor = "darkcyan"
	$Host.ui.rawui.foregroundcolor = "white"
	$hosttime = (Get-ChildItem -Path  $PSHOME\PowerShell.exe).CreationTime
	$Host.UI.RawUI.WindowTitle  =  "PowerShell ($hosttime)"
	Clear-Host
}
 
# Calls the Color-Console function
Color-Console

2. 现在,运行以下命令来重新加载您的 PowerShell 配置文件。

 . $profile

您将看到 PowerShell 控制台上反映了您的个人资料的以下更改。

[玩转系统] PowerShell 配置文件:入门指南

3. 最后,运行您在 PowerShell 配置文件中设置的 tn 别名(而不是 Test-NetConnection cmdlet)来测试您的网络连接。同样,您可以使用 np 打开记事本。

# Test network connection
tn
# Open notepad
np

[玩转系统] PowerShell 配置文件:入门指南

为参数提供默认值

与其重复指定难以记住的特定参数值,为什么不使用 $PSDefaultParameterValues 首选项变量呢?此功能很方便,因为它允许您为 cmdlet 或函数指定自定义值。

$PSDefaultParameterValues 首选项变量是一个哈希表,其条目分为两部分:键和值。键必须与模式 cmdlet:parameter 匹配,其中值可以是参数(字符串、布尔值或整数)或脚本块。

使用 $PSDefaultParameterValues 首选项变量的一般语法如下:

$PSDefaultParameterValues=@{"CmdletName:ParameterName"="DefaultValue"}

要查看 $PSDefaultParameterValues 首选项变量的实际作用:

1. 运行以下命令,该命令不提供输出,但将所有命令的 Verbose 参数设置为 True (*)。新条目将添加到 $PSDefaultParameterValues 哈希表中。

$PSDefaultParameterValues=@{"*:Verbose"=$True}

2. 接下来,运行不带参数的 $PSDefaultParameterValues 变量来验证默认参数变量值。

$PSDefaultParameterValues

[玩转系统] PowerShell 配置文件:入门指南

现在,运行以下 tn 别名来测试您的网络连接。

tn

在下面,您可以看到命令运行时的详细详细信息,因为所有命令的 Verbose 参数值默认设置为 True。

[玩转系统] PowerShell 配置文件:入门指南

结论

在本教程中,您了解了 PowerShell 配置文件的多个功能并更改了 PowerShell 控制台的外观。但更重要的是,您了解了 PowerShell 如何让您定义和重用常用脚本。

有了这些新发现的知识,如果您主要是 PowerShell 用户,为什么不添加新的别名和脚本以在启动时运行以协助您的工作流程呢?配置 PowerShell 配置文件确实是一个救星!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯