Welcome › Forums › General PowerShell Q&A › How to call a function in another file ?
- This topic has 4 replies, 3 voices, and was last updated 10 months, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
March 6, 2020 at 2:12 pm #208800PowerShell12345678910111213[crayon-600ea32af3787341673726 inline="true" ]# file library.ps1Function enviar {param($para)Write-Host $para}# file command.ps1enviar "Hello"
-
This topic was modified 10 months, 3 weeks ago by
vpmaciel67.
-
This topic was modified 10 months, 3 weeks ago by
-
March 6, 2020 at 2:20 pm #208809PowerShell12345678910111213# file library.ps1Function enviar {param($para)Write-Host $para}# file command.ps1. .\library.ps1enviar "Hello"
-
March 6, 2020 at 2:35 pm #208815
A “library” is a basically a Powershell module.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-7
https://docs.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-script-module?view=powershell-7Another option is dot sourcing, like this
PowerShell12345678910111213# file enviar.ps1Function enviar {param($para)Write-Host $para}# file command.ps1.\enviar.ps1 "Hello"If it’s something that you use all of the time, you can also use a Powershell profile to keep common functions available when the Powershell session loads on you’re main computer.
-
March 6, 2020 at 2:54 pm #208833
Another option is dot sourcing, like this
Are you sure that would work this way? I’d expect this way you provide the parameter to the script and not to the contained function.
-
March 6, 2020 at 5:09 pm #208866
Are you sure that would work this way? Iād expect this way you provide the parameter to the script and not to the contained function.
@Olaf, I’m not sure I even be talking to you, I see above your thread has been reported š
You are correct, it would be more like this:
PowerShell12345678910# file enviar.ps1param($para)Write-Host $para# file command.ps1.\enviar.ps1 "Hello"I typically would still lean towards a Module, but you could load a script into memory to load functions.
-
-
AuthorPosts
- The topic ‘How to call a function in another file ?’ is closed to new replies.