10

I have written a powershell script which is a complete function taking parameters (e.g. function name (param) { } ) and below this is a call to the function, with the parameter.

I want to be able to call this function in its .ps1 file, passing in the parameter. How would I be able to package a call to the function via a .bat or .cmd file? I am using Powershell v2.0.

3 Answers 3

17

You should use so called "dot-sourcing" of the script and the command with more than one statement: dot-sourcing of the script + call of the function with parameters.

The test script Test-Function.ps1:

function Test-Me($param1, $param2)
{
 "1:$param1, 2:$param2"
}

The calling .bat file:

powershell ". .\Test-Function.ps1; Test-Me -Param1 'Hello world' -Param2 12345"

powershell ". .\Test-Function.ps1; Test-Me -Param1 \"Hello world\" -Param2 12345"

Notes: this is not a requirement but I would recommend enclosing the entire command text with double quotation marks escaping, if needed, inner quotation marks using CMD escape rules.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked great. Just needed to wrap the path of the ps file with ' so that spaces in the path don't prevent it from working. So powershell.exe ". 'C:\path to\Test-Function.ps1'; Test-Me -Param1 'Hello world' -Param2 12345"
0

I believe all you have to do is name the parameters in the call to the script like the following:

powershell.exe Path\ScripName -Param1 Value1 -Param2 Value2

Param1 and Param2 are actual parameter names in the function signature.

Enjoy!

1 Comment

How do I call the actual function though? Will this call my function? What if I have >1 function in the same .ps1 file with the signature in terms of parameters?
0

To call a PowerShell function from cmd or batch with arguments you need to use the -Commmand Parameter or its alias -C.

Romans answer will work with PowerShell 5.1 for example but will fail for PowerShell 7.1.

Quote from an issue I left on GitHub on why the same command didn't work is:

So as to support Unix shebang lines, pwsh's CLI now defaults to the -File parameter (which expects only a script-file path), whereas powershell.exe default to -Command / -c. To make your commands work with pwsh, you must use -Command / -C explicitly.

So if you have a PowerShell file test.ps1 with:

function Get-Test() {
  [cmdletbinding()]
  Param (
    [Parameter(Mandatory = $true, HelpMessage = 'The test string.')]
    [String]$stringTest
    )
  Write-Host $stringTest
  return
}

And the batch file will then be:

rem Both commands are now working in both v5.1 and v7.1.
rem v7.1
"...pathto\pwsh.exe" -NoExit -Command ". '"...pathto\test.ps1"'; Get-Test ""help me"""
rem v5.1
powershell.exe -NoExit -Command ". '"...pathto\test.ps1"'; Get-Test ""help me"""

The quotes around ...pathto\test.ps1 are a must if your .ps1 contains spaces.

The same goes for ...pathto\pwsh.exe


Here's the Github issue I posted in full:

https://github.com/PowerShell/PowerShell/issues/15281

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.