Question Locked

Powershell check for empty string??

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am having a problem with a powershell script. THis is a snippet of the code and the error that is generate when it is run.

if (($AddlUserDirectoryPath1 -ne $Null) -and ($AddlUserDirectoryPath1 -ne ""))
{
if (!(Test-Path -path $AddlUserDirectoryPath1))

cannot bind argument to parameter 'Path' because it is an empty string.

I am not sure on how to make sure that the string is not empty before testing the path.

Any suggestions will be appreciated.
Thanks,
Art

4 replies

by jimbec59

You can try something like:
$ErrorActionPreference = "SilentlyContinue
if (($AddlUserDirectoryPath1 -ne $Null) -and ($AddlUserDirectoryPath1 -ne $Null)) {
if (!(Test-Path -path $AddlUserDirectoryPath1)) {
"Do this"
}
}

Or if using PS 2.0, try {#Function} catch {#Error handling}
@:-)

by Member 25348569

function IsData($variable) {if ($variable) {$true} else {$false}}

Returns true if the variable has a value, false if it doesn't.

If (IsData $AddlUserDirectoryPath1) {

Neil :)

by awellman

Thanks,
This works in my script. I appreciate the help.
Art

This discussion is closed to new replies.