如何将 PowerShell cmdlet 或函数添加到我的计算机以使其始终可用?
如果我找到(或创建)新的 PowerShell cmdlet(或函数),如何将其添加到我的计算机?
- 我是否将其复制到特定文件夹?
- 我是否将其内容放入特定文件中?
- 我是否需要授权、签名或以某种方式给予许可?
我不想只在一次会话中使用它;我希望每当我在此计算机上使用 PowerShell 时它都可用。
If I find (or create) a new PowerShell cmdlet (or function), how do I add it to my machine?
- Do I copy it to a particular folder?
- Do I put its content in a particular file?
- Do I need to authorize it, or sign it, or give it permission in some way?
I don't want to use it in just one session; I want it to be available whenever I use PowerShell on this machine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Alex 提到的,您的配置文件中或“点”到您的配置文件中的脚本中定义的任何函数将始终可用。如果您在配置文件中使用 Add-PSSnapin 添加管理单元,情况也是如此。管理单元中的 cmdlet 将始终可用。有关配置文件的更多信息,请查看帮助主题:
但是,如果您有大量函数,您可能不想在需要时加载它们。在这种情况下,您可以将功能组织到脚本中,然后将这些脚本放入路径中的一个或多个目录中。然后,您可以按名称引用脚本,而无需指定完整路径,甚至无需指定 .PS1 扩展名。有关使用脚本的更多信息,请查看帮助主题:
PowerShell V2 引入了一种更好的方法来组织函数并按需加载它们。该功能称为“模块”,允许您通过简单的名称(而不是路径)导入模块,并选择哪些函数和变量公开,哪些函数和变量保持私有。如果您有 V2,请查看模块:
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:
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:
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:
您应该通过您的配置文件脚本访问 cmdlet。这样,每次您访问 PowerShell 时,它都会被加载。请参阅配置文件的力量 。
You should access the cmdlets through your profile script. That way, every time you access PowerShell, it gets loaded. See The Power of Profiles.