Script Parameters/Arguments

So let’s take example that I have two lines on my hello.ps1 script

  1. Write-Output "Good morning"
  2. Write-Output "Good Evening"
Goal would be that when running my ps.1 script without parameters, it would read the first line and if i run the script with X parameter it would read the 2nd line. It should also work if those line are also on middle of the script.

Can i it be done something like this:

Write-Output “Good morning” + $Args[0]
Write-Output “Good Evening” + $Args[1]

So if you call the script above like .hello.ps1 , it will return:

“Good morning”

and if you call the script above like .hello.ps1 “Evening” , it will return:

“Good Evening”

Param( [Parameter(Mandatory=$false)]$Greeting = 'morning' )
"Good $Greeting"

you can easily do this by simply not wrapping your parameters in a function, then supplying a default message. In this case, the script itself would take the parameters.

Script could look like this
[pre]
param(
[string] $Message = “Good Morning!”
)

Write-Output -InputObject $Message
[/pre]
and would output
[pre]
PS > .\Script.ps1
Good Morning!
[/pre]

Then you could change the message by specifying at runtime:
[pre]
PS > .\Script.ps1 -Message “Good Evening!”
Good Evening!
[/pre]

Thanks this was just what i was looking for !