Author Posts

February 8, 2018 at 1:24 am

This works fine, .\Untitled2.ps1 -Computer hello

Param(
[string]$Computer,
[string]$filepath
)

"You entered '$Computer' and '$filepath'"

However when I put anything before Param, "param" becomes a unrecognizable command.

Shouldn't this work below , my aim is trying to Clear-Host before the "Param"

Clear-Host 

Test

function Test {
    
Param(
[string]$Computer,
[string]$filepath
)

}


"You entered '$Computer' and '$filepath'"

February 8, 2018 at 1:29 am

Param blocks have to be defined before almost anything else. That's the rule.

But your second example fails because, when you called Test, the function had not yet been defined. PowerShell executes top to bottom.

February 8, 2018 at 1:41 am

I'm curious ...

... my aim is trying to Clear-Host before the "Param"

Why? What for?

February 8, 2018 at 2:36 am

Im using PowerShell ISE, and I have other stuff in the console previously so I want to clear the screen.

February 8, 2018 at 4:37 am

ISE, console host, VSCode, etc...

What you are after is this.
Use Clear-Host during the call to the function, not before you call the function.

    function Test {
    
    Param(
    [string]$Computer,
    [string]$filepath
    )

    }

    Clear-Host
    Test 

    # Or 

    Clear-Host;Test

February 8, 2018 at 7:07 am

Hmmm ... if it's because you want to see the output of that particular function without anything before it and you really want it to be hard coded in your function you can place your Clear-Host right after the param block like this:

function foo{
    Param(
    [string]$Computer,
    [string]$filepath
    )
    Clear-Host
    'some stuff'
    'Some other stuff'
}

foo