Is there a way for a batch file to call a powershell function? I've tried
powershell ". .\tes.ps1; Test-Me -Param1 'Hello world' -Param2 12345"
And it works, the function gets called, but so does everything else in the powershell script.
It looks like what you're trying to do is dot-source tes.ps1 so that you can use the Test-Me function that's defined in that file. When you dot-source a file, everything in that file gets executed. If you have other commands in tes.ps1 that you don't want to execute, then you'll need to put Test-Me in a separate file. The best way to do that: Create a file called Test-Me.ps1 that contains the contents of the function (don't declare a function with function { [...] }, just include the code inside the function's scriptblock), then invoke it like this in your batch file:
powershell "<path>\Test-Me.ps1 -Param1 'Hello world' -Param2 12345"
-File option: powershell -File "C:\path\to\Test-Me.ps1" -Param1 "Hello world!" -Param2 12345.@Adi Inbar 's solution works perfect.
Here is another way (more from structuring standpoint): If tes.ps1 only contains functions, create a brand new script with following contents:
.\tes.ps1
Test-Me -Param1 'Hello world' -Param2 12345
Then in batch file, run this brand new script with the "file" parameter:
Powershell -file *path_to_PS_script*
The tes.ps1 run first it will load all functions in the script scope. You can pick which function to run.
In this way tes.ps1 file serves as a central library of functions.