Hi ,
I have written a function in powershell and i want to share that with someone to use as cmdlet, how can i do that
Technology Tips and News
Hi ,
I have written a function in powershell and i want to share that with someone to use as cmdlet, how can i do that
Hi,
Write yours funtions in a ps1 file and rename it to psm1 file.
You will call all functions in your file with the command :
Import-Module "C:\NAME-PSMODULE.psm1"
Regards,
Follow this steps :
- Create you PSM1 file with your function with this form :
function Test-Connection { return $true }
- In the beginning of your script, call you module with this command :
Import-Module "PATH-OF-YOUR-FILE\NAME-PSMODULE.psm1"- Call your function normally
For exemple i call my function test-connection
If ((Test-Connection) -eq $true){ }
Hi PA,
you can make a function available by saving them to disk in a .ps1 file and then importing it into a powershell console using Import-Module.
For example you could download this script file - which contains a function - then import it into a PowerShell session (you'll likely need to unblock it first). This will allow you to use this function as long as the console is running.
The documentation for the linked script will tell you all you need to use it, you can use the same methods for your own functions.
Cheers,
Fred
copy the psm1 to their machine C:\Windows\System32\WindowsPowerShell\v1.0\Modules PSM1 file and folder name must match.
With wmf 4.0 modules are imported automatically when you type the cmdlet.
Hi Powershell-Admin,
As Mickal suggested, you can save the function as .psm1 file, and share this file to others.
Then import the function by the cmdlet: "Import-Module d:\yourscript.psm1"
For more detailed information, please also refer to this article:
http://www.thomas-franke.net/including-scripts-functions-modules/
If there is anything else regarding this issue, please feel free to post back.
Best Regards,
Anna Wang