param ([ValidateScript({ Test-Path -Path $_ -PathType Leaf })][string]$filePath)
If I declare a parameter like this, will $filePath
evaluate to false if it's an invalid path?
Is the point of this to do something like
if($filePath) { /* do stuff... */ }
or will an exception be thrown?
You should use the ValidateScript
attribute if your function requires a valid path. PowerShell will throw the error for you if the user provides an invalid path. You probably also want to add [Parameter(Mandatory=$true)]
otherwise you can omit the $filePath
parameter and the function will get called without an exception.
Here is an example:
function This-IsYourFunction
{
Param
(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string]
$filePath
)
Write-Host "Hello, World."
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With