如何创建PowerShell配置文件

PowerShell is a great way to automate almost anything in Windows. However, its not just a scripting language. If you find yourself using it as a command line shell it may be useful to store your functions and customizations in a profile that gets loaded every time you load the Console. Read on to find out how.

PowerShell是自动执行Windows中几乎所有操作的好方法。 但是,它不仅是一种脚本语言。 如果发现自己将其用作命令行外壳,则将功能和自定义存储在每次加载控制台时都会加载的配置文件中可能会很有用。 阅读,了解如何。

创建PowerShell配置文件 (Creating a PowerShell Profile)

The first thing we need to do is check if you already have a profile. There is an automatic variable, $Profile, that stores the fully qualified location of the PowerShell profile. An easy way to check if your profile exists is to use the Test-Path cmdlet on the $Profile variable.

我们需要做的第一件事是检查您是否已有个人资料。 有一个自动变量$ Profile,用于存储PowerShell概要文件的完全限定位置。 一种简单的检查个人资料是否存在的方法是在$ Profile变量上使用Test-Path cmdlet。

Test-Path $Profile

测试路径$ Profile

image

As you can see we don’t have a profile file yet, so we will need to create one, you can easily do that with the New-Item cmdlet.

如您所见,我们还没有配置文件,因此我们需要创建一个配置文件,您可以使用New-Item cmdlet轻松地进行配置。

New-Item –Path $Profile –Type File –Force

新项目–路径$ Profile –类型文件–强制

Note: Using the force parameter will cause a profile to be created even if you already had one. This means your old profile will be overwritten.

注意:即使您已经拥有一个概要文件,使用force参数也将导致概要文件的创建。 这意味着您的旧个人资料将被覆盖。

image

You can edit your profile using notepad, which is easily started using PowerShell.

您可以使用记事本编辑个人资料,使用PowerShell可以轻松启动记事本。

notepad $Profile

记事本$ Profile

image

You can put any commands,functions,alias’s and even module imports into your PowerShell profile. Here are some things examples and ideas of what you can put in your profile.

您可以将任何命令,功能,别名甚至模块导入到PowerShell配置文件中。 以下是一些示例和您可以在个人资料中添加的内容的想法。

Since PowerShell 3 comes with updatable help, the easiest way to keep your help files updated is by adding the Update-Help cmdlet to your profile.

由于PowerShell 3附带了可更新的帮助,因此,使帮助文件保持最新状态的最简单方法是将Update-Help cmdlet添加到配置文件中。

Note: Update-Help will only download help files once a day, this is fine for us since we don’t want it updating help files every single time we open the console. If you do want it to update every time you can use the force parameter.

注意:Update-Help每天只会下载一次帮助文件,这对我们来说很好,因为我们不希望每次打开控制台时都更新帮助文件。 如果确实希望它每次都更新,则可以使用force参数。

image

Another thing I like to add to my profile is custom functions that I have written over time, this makes them automatically available in the console. Below you can see that you can literally just copy a function out of a script and put it in your profile. It will then be available for use in the console.

我想添加到个人资料中的另一件事是随着时间的推移编写的自定义函数,这使它们在控制台中自动可用。 在下面可以看到,您可以从字面上直接复制一个函数并将其放入您的配置文件中。 然后它将可以在控制台中使用。

image

Finally, I also have some customizations to the console. One of my favorite ones is shown below, it basically determines if you have opened an elevated PowerShell console and changes the font color, this way I always remember that I am running with elevated privileges.

最后,我还对控制台进行了一些自定义。 我最喜欢的一个如下所示,它基本上确定您是否打开了提升的PowerShell控制台并更改了字体颜色,这样我就始终记得我以提升的特权运行。

image

What do you have in your profile? Let us know in the comments.

您的个人资料中有什么? 让我们在评论中知道。

翻译自: https://www.howtogeek.com/126469/how-to-create-a-powershell-profile/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值