PowerShell script parameters – matching a known filetype

Just a quick note for today as I was writing an automated recursive delete function and wanted to ensure I didn’t accidently read in another script and attempt to delete any path it contained!:

Add this to the top of your script to require a mandatory “.txt” file as a parameter

[ValidateScript({$_ -match ‘[^.]\.txt+$’})] #regular expression to make sure its a text file that is being read in
[string]$inputfilepath #Read in path to text file of folders to delete


[^.] Matches characters that are not a .

\. Matches a .

txt is the filetype extension we are looking for

+$ signifies this is the end of the string

Leave a Reply

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