Powershell-Parameters



Script Parameters / Arguments (for scripts, functions and script blocks) To pass arguments to a script or cmdlet type them separated by spaces:

You just have to know how to declare your parameters.


Scenario 1:

Example


Param(
[string]$computerName,
[int]$Number
)
    

Copy and Try it


Scenario 2: I could use the parameters like this:

Example


Get-Something -computerName SERVER1 -filePath C:\Whatever
    

Copy and Try it

You can do the parameter validation using PowerShell.


ValidateNotNull only checks to see if the value being passed to the parameter is a null value. Empty string is still valid.
ValidateNotNullorEmpty This also checks to see if the value being passed is a null value and if it is an empty string or collection

Example


    Function TestingMethod {
    [cmdletbinding()]
    Param(
        [parameter(ValueFromPipeline)]
        [ValidateNotNull()] #No value
        $Item )
    Process {
                $Item
            }
    }

Copy and Try it