What is the best way to take a file path as a parameter?


  • I am currently taking the file path as a String parameter using the following code to validate the file extension is correct.

    [Parameter(Mandatory=$true, Position=0, ParameterSetName="Path")]
    [ValidateScript({($_.EndsWith(".xaml") -and (Test-Path $_))})]

    Is there a better way to ensure that the parameter provided is a valid path to an existing file of a certain extension?

    Thanks in advance!

    Thursday, May 17, 2018 4:39 PM


All replies

  • What is wrong with the method you are using?


    Thursday, May 17, 2018 5:31 PM
  • Nothing is wrong with it from my view, however I have no means of consulting with other developers in my current role and I'm cobbling together code found out and about on the internet. This particular solution felt wrong as I've never implemented code as part of a parameter declaration before, and I felt there might be a more effective solution.

    I suppose a better way to phrase the question would be, "Is there anything wrong with doing [thing]?"

    Thursday, May 17, 2018 5:52 PM
  • No.  That is what ValidateScript is for.


    • Marked as answer by natpalmer1776 Thursday, May 17, 2018 5:55 PM
    Thursday, May 17, 2018 5:53 PM
  • Thanks for putting up with the inane questions.
    Thursday, May 17, 2018 5:55 PM
  • See: help about_Functions_Advanced_Param


    Thursday, May 17, 2018 5:58 PM