如何将 PowerShell cmdlet 或函数添加到我的计算机以使其始终可用? [英] How do I add a PowerShell cmdlet or function to my machine so that it is always available?

查看:44
本文介绍了如何将 PowerShell cmdlet 或函数添加到我的计算机以使其始终可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我找到(或创建)一个新的 PowerShell cmdlet(或函数),我该如何将它添加到我的机器上?

If I find (or create) a new PowerShell cmdlet (or function), how do I add it to my machine?

  • 我是否将其复制到特定文件夹?
  • 我是否将其内容放在特定文件中?
  • 我是否需要授权、签署或以某种方式给予许可?

我不想在一个会话中使用它;我希望每当我在这台机器上使用 PowerShell 时它都可用.

I don't want to use it in just one session; I want it to be available whenever I use PowerShell on this machine.

推荐答案

正如 Alex 所提到的,在您的配置文件或点缀"到您的配置文件中的脚本中定义的任何函数将始终可用.如果您在个人资料中使用 Add-PSSnapin 添加管理单元,情况也是如此.管理单元中的 cmdlet 将始终可用.有关配置文件的更多信息,请查看帮助主题:

As Alex mentions, any function defined in your profile or in a script that gets "dotted" into your profile will always be available. The same goes if you use Add-PSSnapin in your profile to add a snapin. The cmdlets in the snapin will always be available. For more information about profiles check out the help topic:

man about_profiles

但是,如果您有大量函数,您可能不想在需要它们之前加载它们.在这种情况下,您可以将功能组织到脚本中,然后将这些脚本放入您路径中的一个或多个目录中.然后,您可以按名称引用脚本,而无需指定完整路径甚至 .PS1 扩展名.有关使用脚本的更多信息,请查看帮助主题:

However if you have a significant number of functions you may not want to load them until they are needed. In this case, you can organize functionality into scripts and then put those scripts into one or more directories that are in your path. You can then reference the script by name without specifying the full path or even the .PS1 extension. For more information about using scripts check out the help topic:

man about_scripts

PowerShell V2 引入了一种更好的方法来组织函数并按需加载它们.该功能称为模块,允许您通过简单的名称(而不是路径)导入模块,并选择哪些函数和变量是公开的,哪些是私有的.如果您有 V2,请查看模块:

PowerShell V2 introduces an even better approach to organizing functions and loading them on demand. The feature is called Modules and allows you to Import-Module by a simple name (rather than path) and to choose which functions and variable are made public versus which ones remain private. If you have V2, check out modules:

man about_modules

这篇关于如何将 PowerShell cmdlet 或函数添加到我的计算机以使其始终可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆