15

I want to be able to pass a single int through to a powershell script, and be able to tell when no variable is passed. My understanding was that the following should identify whether an argument is null or not:

if (!$args[0]) { Write-Host "Null" }
else { Write-Host "Not null" }

This works fine until I try to pass 0 as an int. If I use 0 as an argument, Powershell treats it as null. Whats the correct way to be able to distinguish between the argument being empty or having a zero value?

3 Answers 3

15

You can just test $args variable or $args.count to see how many vars are passed to the script.

Another thing : $args[0] -eq $null is different from $args[0] -eq 0 and from !$args[0].

  1. $args[0] -eq $null : This checks if the first argument in the $args array is equal to $null. If $args[0] is $null, it returns True; otherwise, it returns False.
  2. $null -eq $args[0] : While this performs the same logical comparison, it is generally safer to use this order. Because if $args[0] is undefined or doesn't exist it prevents PowerShell from trying to evaluate a potentially non-existent value, reducing the risk of exception.
  3. $args[0] -eq 0 : This checks if $args[0] is numerically equal to 0. PowerShell treats numbers differently from $null, so this comparison only returns True if $args[0] contains the number 0. If $args[0] is $null, or is $args[0] is a string, this will return False.
  4. !$args[0] : checks for any "falsy" value, including $false, $null, 0, and an empty string.
Sign up to request clarification or add additional context in comments.

8 Comments

Didn't realise there was a difference between $args[0] -eq $null and !$args[0] - many thanks!
If you use the $Var directly in a condition it evaluates everything that is $Null or 0 to $False.. everything else to $True. So If(-1) is always False.
@MichaelKargl What do you mean by "if(-1) is always false"? -1 evaluates to True (try [bool]-1). Also note that in addition to $null and 0, empty strings also evaluate to False ([bool]"")
My bad.. typo.. its always $True as it is unequal 0.
This answer would be a lot better if it explained the differences between the equality checks. As it is I hear they are different, but I have no idea why. It should tell me how to properly check for empty/null/zero arguments.
|
5

If users like me come from Google and want to know how to treat empty command line parameters, here is a possible solution:

if (!$args) { Write-Host "Null" }

This checks the $args array. If you want to check the first element of the array (i.e. the first cmdline parameter), use the solution from the OP:

if (!$args[0]) { Write-Host "Null" }

Comments

3

If the variable is declared in param() as an integer then its value will be '0' even if no value is specified for the argument. To prevent that you have to declare it as nullable:

param([AllowNull()][System.Nullable[int]]$Variable)

This will allow you to validate with If ($Variable -eq $null) {}

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.