Validating file name input in Powershell
<p>I would like to  validate input for file name and check if it contains invalid characters, in PowerShell. I had tried following approach, which works when just one of these character is entered but doesn't seem to work when a given alpha-numeric string contains these characters. I believe I didn't construct regex correctly, what would be the right way to validate whether given string contains these characters? Thanks in advance.</p>

<pre><code>#Validate file name whether it contains invalid characters: \ / : * ? " &lt; &gt; |
$filename = "\?filename.txt"
if($filename -match "^[\\\/\:\*\?\&lt;\&gt;\|]*$")
    {Write-Host "$filename contains invalid characters"}
else
    {Write-Host "$filename is valid"}
</code></pre>