You Can use ‘throw’ key to respond to a condition or in the Catch block of a
Try-Catch-Finally statement. You can also use the Throw keyword in a
parameter declaration to make a function parameter mandatory.
we use the ‘throw’ for a variable we want it to be filled with required data ,for example i would like the Parameters on my function will be declared by the user –
1 2 3 4 5 6 7 |
function UsingThrow { Param ( $Name = $(throw "-Condition1 is required."), $Age = $(throw "-Condition2 is required.") ) write-host "My Name is $name and my Age is $Age" |
Example 2 –
1 2 3 4 5 6 7 8 |
function Get-Files { param ( $path = $(throw "The Path parameter is required."), $FileExtension = $(throw "The Extension parameter is required.") ) dir -path $path\*.$FileExtension -recurse | sort lastwritetime | ft lastwritetime, attributes, name -auto } |
(Visited 1,187 times, 1 visits today)