[玩转系统] 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 SystemPowerShell 配置文件位置
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 配置文件。
事实上,当前用户和所有用户都有不同的配置文件,如下所示。但是,当谈论 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 配置文件的路径。
# 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 的执行策略 (Set-ExecutionPolicy) 设置为 RemoteSigned。
RemoteSigned 策略规定,任何脚本如果不是在运行脚本的系统上创建的,都应该进行签名。此策略要求来自 Internet 或由 Internet Explorer、Outlook 或 Messenger 等应用程序下载的脚本具有数字签名。
Set-ExecutionPolicy RemoteSigned
现在,运行以下任一命令,该命令不提供输出,但在 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 控制台上反映了您的个人资料的以下更改。
3. 最后,运行您在 PowerShell 配置文件中设置的 tn 别名(而不是 Test-NetConnection cmdlet)来测试您的网络连接。同样,您可以使用 np 打开记事本。
# Test network connection
tn
# Open notepad
np
为参数提供默认值
与其重复指定难以记住的特定参数值,为什么不使用 $PSDefaultParameterValues 首选项变量呢?此功能很方便,因为它允许您为 cmdlet 或函数指定自定义值。
$PSDefaultParameterValues 首选项变量是一个哈希表,其条目分为两部分:键和值。键必须与模式 cmdlet:parameter 匹配,其中值可以是参数(字符串、布尔值或整数)或脚本块。
使用 $PSDefaultParameterValues 首选项变量的一般语法如下:
$PSDefaultParameterValues=@{"CmdletName:ParameterName"="DefaultValue"}
要查看 $PSDefaultParameterValues 首选项变量的实际作用:
1. 运行以下命令,该命令不提供输出,但将所有命令的 Verbose 参数设置为 True (*)。新条目将添加到 $PSDefaultParameterValues 哈希表中。
$PSDefaultParameterValues=@{"*:Verbose"=$True}
2. 接下来,运行不带参数的 $PSDefaultParameterValues 变量来验证默认参数变量值。
$PSDefaultParameterValues
现在,运行以下 tn 别名来测试您的网络连接。
tn
在下面,您可以看到命令运行时的详细详细信息,因为所有命令的 Verbose 参数值默认设置为 True。
结论
在本教程中,您了解了 PowerShell 配置文件的多个功能并更改了 PowerShell 控制台的外观。但更重要的是,您了解了 PowerShell 如何让您定义和重用常用脚本。
有了这些新发现的知识,如果您主要是 PowerShell 用户,为什么不添加新的别名和脚本以在启动时运行以协助您的工作流程呢?配置 PowerShell 配置文件确实是一个救星!
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag