Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a PowerShell cmdlet or function to my machine so that it is always available?

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.

like image 449
Leon Bambrick Avatar asked Aug 28 '09 01:08

Leon Bambrick


People also ask

How do I get the PowerShell cmdlet?

To display help for a specific PowerShell cmdlet: Open Windows PowerShell or Windows PowerShell Integrated Scripting Environment (ISE). Type Get-Help <cmdlet>, for example, Get-Help Publish-AppvClientPackage.

How do I add a function in PowerShell?

A simple functionA function in PowerShell is declared with the function keyword followed by the function name and then an open and closing curly brace. The code that the function will execute is contained within those curly braces.

Where are PowerShell cmdlets stored?

By default, on Windows 10 and higher, that location is $HOME\Documents\PowerShell\Modules . On Linux or Mac, the CurrentUser location is $HOME/. local/share/powershell/Modules .


2 Answers

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

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 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
like image 135
Keith Hill Avatar answered Oct 11 '22 15:10

Keith Hill


You should access the cmdlets through your profile script. That way, every time you access PowerShell, it gets loaded. See The Power of Profiles.

like image 26
Alex Avatar answered Oct 11 '22 15:10

Alex