Functions

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

June 30th, 2015 7:21am

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,

Free Windows Admin Tool Kit Click here and download it now
June 30th, 2015 7:24am

i dont get you :( should i call my function in .ps1? 
June 30th, 2015 7:29am

Tell them to add it to their profile :-) 

Learn about Profiles here



Free Windows Admin Tool Kit Click here and download it now
June 30th, 2015 7:34am

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){

}


June 30th, 2015 7:37am

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

Free Windows Admin Tool Kit Click here and download it now
June 30th, 2015 7:38am

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.

June 30th, 2015 1:06pm

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

Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 5:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics