Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PowerShell, can Test-Path (or something else) be used to validate multiple files when declaring a string array parameter

Tags:

powershell

I have a function that accepts a string array parameter of files and I would like to use Test-Path (or something else) to ensure that all the files in the string array parameter exists. I would like to do this in the parameter declaration if possible.

Is this possible?

like image 603
Benoit Drapeau Avatar asked Oct 24 '14 22:10

Benoit Drapeau


People also ask

What does test-path do in PowerShell?

The Test-Path cmdlet determines whether all elements of the path exist. It returns $True if all elements exist and $False if any are missing. It can also tell whether the path syntax is valid and whether the path leads to a container or a terminal or leaf element.

How do you validate parameters in PowerShell?

The first option for validating parameters in PowerShell is the ValidateSet-attribute. It allows you to specify a set of valid values for a parameter or variable and additionally enables tab completion in the shell.

What is Property validation attribute in PowerShell?

Parameter and variable validation attributes. Validation attributes direct PowerShell to test the parameter values that users submit when they call the advanced function. If the parameter values fail the test, an error is generated and the function isn't called.


1 Answers

You can use ValidateScript

param(
[parameter()]
[ValidateScript({Test-Path $_ })]
[string[]]$paths
)

For more documentation on parameter validation visit about_Functions_Advanced_Parameters

like image 160
Paul Avatar answered Sep 19 '22 11:09

Paul