Powershell check for empty string??

By awellman ·
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.

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Here is something to try..

by jimbec59 In reply to Powershell check for empt ...

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}

Collapse -

Here you go

by neilb@uk In reply to Powershell check for empt ...

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 :)

Collapse -


by awellman In reply to Here you go

This works in my script. I appreciate the help.

Back to Web Development Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums