0

I have a script with this function inside:

function init
{
Write-Output "test output"
}

I want to call it from the command line (not from PS), but these do not work:

C:\Scripts>powershell -command "& { c:\Scripts\sc.ps1; init }"
init : The term 'init' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:17
+ .\sc.ps1; init
+           ~~~~
+ CategoryInfo          : ObjectNotFound: (init:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
C:\Scripts>powershell -file c:\Scripts\sc.ps1 -command init
C:\Scripts>
C:\Scripts>powershell -file .\sc.ps1 -command init
C:\Scripts>

Execution policy is unrestricted.

Hints from: Calling a specific PowerShell function from the command line

Wasif
  • 14,755
  • 3
  • 14
  • 34
user1156544
  • 1,725
  • 2
  • 25
  • 51
  • 2
    Just a typo: dot-sourcing is missing the dot: `{ c:` -> `{ . c:` – vonPryz Feb 26 '20 at 11:00
  • Thanks. So silly! I should paid more attention to the accepted answer and not to the others. This works: `powershell -command ". .\sc.ps1; init"` – user1156544 Feb 26 '20 at 11:06

0 Answers0