0

I am trying to create a powershell script that will append a character to my variable only if the variable is not null. For instance a user fills out a form, or spreadsheet cell in my case and it has data I want to append a | to that variable so when I call that variable in my script it will display | contents of variable. This is what I have and it does not seem to be working.

 if ([string]::IsNullOrEmpty($field6))
  {$field6 = $field6}
 else 
  {$field6 = "| $field6" }

  }
echo $field6

1 Answer 1

3

This should suffice:

if ($field6 -ne $null) { $field6 = "| $field6" }
Sign up to request clarification or add additional context in comments.

2 Comments

You can even skip the explicit null check: if ($field6) { $field6 = "| $field6" }
@mousio That may yield undesired results, because there are other values than $null that will make if ($field6) evaluate to $false.

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.