Accepting file and folder paths as a parameter is a pretty common occurrence in PowerShell scripts. Without validating these parameters, your script might not work as indented.

Micah Rairdon

I am a PowerShell fanatic currently working in Windows client management at Haworth based out of Holland, MI. I tinker around and document my findings at tiberriver256.github.io and you can find me on Twitter @tiberriver256.

Typically, you might accept path parameters to your function as follows:

The problem ^

The problem with accepting the parameter is that you can't be certain a function call passes an actual file path. We are accepting anything that is a string, and we are going to start doing things with that string, trusting that the script user did not make any mistakes when typing in the path. This could lead to unexpected results.

For instance, if your script prompts for a folder path, and the user enters something unexpected, your script won't be able to perform your intended task. In the example below, the script can't create the folder if the user enters illegal characters for folder names.

Accepting user input without validation can cause problems

Accepting user input without validation can cause problems

The best practice is to anticipate what type of file path we expect the user to pass. We can then validate that before executing any code that could possibly make unexpected changes to the system.

The solution ^

Let's begin by asking some questions about the path we expect:

  • Should the path always point to a file or always be a folder—or do we take both?
  • Should the file or folder exist or not?
  • If it is always going to be a file and not a folder, what kind of file extensions are we looking for?
  • If we are creating files/folders, how do we validate no illegal characters are in the path?

Now that we have the basic questions hopefully answered for the type of path we expect for the argument, we can now begin to write validation attributes for our parameter. Validation attributes are attributes you can add to parameters when writing a PowerShell function (for an excellent introduction see this 4sysops blog post by Adam Bertram).

Of the eight available validation attributes, the most useful for this use case would be the ValidateScript parameter. This will also allow us to return helpful error messages to the user in case the path did not pass validation.

Below I will explain how you can validate parameters in PowerShell with or without the help of validate scripts.

Validate the path does not contain Illegal characters ^

We don't need any validate scripts for this task. The .NET class System.IO.FileInfo will take care of this task for us.

If you pass an invalid path as an argument, PowerShell will generate this error message:

Validate the path exists ^

We can do this relatively simply using Test-Path. You can also easily reverse this to validate that a file or folder does not exist.

If passing a folder or file that does not exist as an argument, you will receive this error message:

Validate the path exists and is a file or a folder

We can use the PathType parameter of the Test-Path cmdlet to verify the path is either a leaf (file) or container (folder) type. It is also helpful to perform each test separately so the error returned remains very specific.

And this is how the error message looks:

Validate the file extension ^

If you have researched a little bit into the validation parameters, you may know already that we can use the ValidatePattern attribute to accomplish this task easily. However, ValidateScript allows us to display a more descriptive error message.

If the filename does not have the required extension, you will see this error message:

Conclusion ^

Validating file and folder paths is essential because otherwise your PowerShell script might produce unexpected and unwanted results. You can use built-in PowerShell features to validate user input, or you can write a validate script.

Win the monthly 4sysops member prize for IT pros

Share
2+

Users who have LIKED this post:

  • avatar

Related Posts

2 Comments
  1. Luc Fullenwarth 6 months ago

    I was using validation attributes and casting since a while,
    but wasn't aware that casting is also a validation method.

    Thanks for you good post Micah!

    1+

    Users who have LIKED this comment:

    • avatar
  2. Wojciech Sciesinski 6 months ago

    For advanced file name creation and validation (e.g. if a file/folder exist) you can use the New-ObjectOutput module https://www.powershellgallery.com/packages/New-OutputObject/

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account