1

I can use Test-Path to check whether the file name entered exists, but I'd like to avoid producing a system error if the user hits RETURN and the input string is blank. I thought the -ErrorAction Common Parameter would do the trick, but this:

$configFile = Read-Host "Please specify a config. file: "
$checkfile = Test-Path $configFile -ErrorAction SilentlyContinue

still produces:

Test-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At C:\Scripts\testparm2.ps1:19 char:31
+         $checkfile = Test-Path <<<<  $configFile -ErrorAction SilentlyContinue
    + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

Do I have to check that the string isn't blank or NULL explicitly?

I'm using PowerShell v2.0

2
  • if you check Ansgar Wiechers answer... your code returns $true when you pass a 0 Commented Jul 14, 2015 at 13:54
  • Just to avoid confusing innocent bystanders: 0 and null are different values with different meanings. Commented Jul 14, 2015 at 14:04

2 Answers 2

2

You can do something like this:

$checkfile = if ("$configFile") {
               Test-Path -LiteralPath $configFile
             } else {
               $false
             }

The double quotes prevent false negatives, e.g. in case you want to test for the existence of a folder named 0.

Another option would be to set $ErrorActionPreference. However, in that case you need to cast the result of Test-Path to a boolean value, because although the exception is suppressed the cmdlet still doesn't return a result. Casting that $null "return value" to bool produces $false.

$oldEAP = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'

$checkfile = [bool](Test-Path -LiteralPath $configFile)

$ErrorActionPreference = $oldEAP
Sign up to request clarification or add additional context in comments.

5 Comments

Interesting "false negative" when passing 0 as parameter
@cad A numeric value of 0 would be interpreted as $false whereas a string "0" wouldn't (because it's not empty). See here for details.
@cad @Ansgar Wiechers, Thanks to both of you. Can you explain why the -ErrorAction clause didn't work?
It should... I don't know why.
@rojomoke I would suspect that the parameter binding errors are not covered by the error action (since -ErrorAction is a parameter too). You can set $ErrorActionPreference = 'SilentlyContinue' instead.
2

Yes, you have to check explicitly for string null or empty:

$configFile = Read-Host "Please specify a config. file: "
if ([string]::IsNullOrEmpty($configFile))
{
    $checkfile = $false
}
else 
{
    $checkfile = Test-Path $configFile -ErrorAction SilentlyContinue
}

Or use try/catch:

$configFile = Read-Host "Please specify a config. file: "
if ( $(Try { Test-Path $configFile.trim() } Catch { $false }) ) 
{
   $checkfile = $true
}
else 
{
   $checkfile = $false
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.