If you are running the script by F5 or F8 in the ISE it’s supposed to show up in the console, this shows the code is loaded and thus will execute.
This will happen whether you use cod inside of out side of a function and run from the ISE. This is the same in VSCode, actually this happens with any development tool.
To prevent cold from showing up in the console on normal runs, then that code must be a function, that is pre-loaded (which when you do this, it will show up in the console at least once.), then run the code using just the function name
If you are loading the code and running it at the same time, then you can use Clear-Host to remove the code from the screen and only show the results.
# Load the function
function My-Function ($param1, $param2)
{
'Hello World'
}
# Clear the screen
Clear-Host
# Run the function
My-Function
# Results
Hello World
Saving the code as a script…
# Load the function
function My-Function ($param1, $param2)
{
'Hello World'
}
# Run the function
My-Function
… as a .ps1 and running the .ps1, will load the code and run the function.
It will not display the function code in the console.
.\My-Function.ps1
Hello World
If you are not using a function in the script…
Send a message
‘Hello World’
… in the ISE / VSCode, using F5 (Run)…
D:\Scripts\My-Function.ps1
Hello World
ISE / VSCode - Selecting the code and using F8 (Run selection)
# Send a message
'Hello World'
Hello World
Running this as a script from the console
.\My-Function.ps1
Hello World