I have a script. Saved it as Functionssample.ps1 and called it from powershell command prompt. I have set executionpolicy to unrestricted and ran the powershell as "Administrator"
.\Functionssample.ps1 'Display-HelloWorld'
I don't see "hello world" on console.
Function Display-HelloWorld { cls Write-Host "Hello World" } 13 Answers
You've written a script with a function in it. Since scripts can accept parameters, the call .\functionsample.ps1 Display-HelloWorld is simply passing "Display-HelloWorld" (a string value) as a parameter to the script.
When the script executes, it creates a function object, but the function is never called.
To execute your function, you need to dot-source the script (note the extra dot and space)
. .\functionsample.ps1 This runs the script in your current scope so that the function is available after the script is done.
Then, you can call it.
Display-HelloWorld Your choices are:
1) Write a script containing the function, and the code which calls it, in a file called anything - here 'testing.ps1':
Function Display-HelloWorld { cls Write-Host "Hello World" } Display-HelloWorld # run it PS C:\test> .\testing.ps1 Hello World # After it runs, the contents of the script are 'gone' from the current session. 2) Put the function content in a file called functionname.ps1. Treat the script itself as a function:
cls Write-Host "Hello World" # run it, pretending the file is a function PS C:\test> .\Display-HelloWorld.ps1 Hello World 3) Put the function code in a file by any name:
Function Display-HelloWorld { cls Write-Host "Hello World" } # dot source or Import-Module the code, to load the file contents ready for use PS C:\test> Import-Module .\testing.ps1 # Call the function, loaded from the file PS C:\test> Display-HelloWorld Hello World You're mixing them up - when you put function xyz in the file, then run the file, it runs as a script. Function is defined. Nothing calls it. Function is removed from memory. When you run the file and pass it a parameter, it's as if you're doing 2), as Mike Shepard says, passing 'Display-HelloWorld' as a string parameter to the file, as if the file itself was a function. It doesn't have a param() block to accept parameters, so it does nothing.
Try this instead in your console:
Import-Module .\Functionssample.ps1 That will load your script and its functions that you have created, so you could now run them in that console. While it isn't a module, it still works for this situation...
Display-HelloWorld Edit: My apologies, my first post had the incorrect command
Another option would be this:
powershell -command "& { . .\script1.ps1; Display-HelloWorld }" it uses dot sourcing to run through the script and then executes the command. If you only do .\Functionssample.ps1 Display-HelloWorld it doesn't actually seem to be invoking the command properly. It will run without error, but it doesn't actually run the function like you're expecting it to. If a more experienced PowerShell user can give an explanation as to why that doesn't work that would be helpful for this post.