ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Powershell "-eq" operator and "False"

    Developer Discussion
    powershell false
    5
    15
    1.4k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • gjacobseG
      gjacobse @Danp
      last edited by

      @Danp said in Powershell "-eq" operator and "False":

      if ($UserE.enabled -eq "False") {

      Try if ($UserE.enabled -eq $False) {

      if ($UserE.enabled -ne "True") {...

      This works, but not in the way you think. It would likely give the incorrect result when enabled is $True.

      It's sort of odd that I rather understand - having 'collected' a script to look at a user (or computer) account and perform certain actions. Specifically - disable one or enable one.

      Thanks for sharing.

      1 Reply Last reply Reply Quote 0
      • 1
        1337 @pmoncho
        last edited by 1337

        @pmoncho said in Powershell "-eq" operator and "False":

        Trying to figure out why this will not work? I'm stumped

        if ($UserE.enabled -eq "False") {
           $a = read-host "Move to Disabled Accounts OU? (Y/N)"
           $answer
        }
        

        You're just confusing the logical boolean type (true or false) with string types (any text). So you're making an invalid comparison. Easy enough mistake since they look alike.

        So it has nothing to do with the if statement or -eq.

        Examples of logically false:

        • number 0
        • empty string '' or ""
        • $null
        • predefined variable $false

        Examples of logically true:

        • anything not 0
        • any string that isn't empty
        • predefined variable $true

        As you can see a string that has the content "False" is a not an empty string. So it is logically the same as true.

        What you should be doing is:

        if ($UserE.enabled) {
           $a = read-host "Move to Disabled Accounts OU? Y/N)"
           $answer
        }
        

        All if-then statements test for logical true automatically. So experienced developers never make an if-then statement explicitly comparing to true or false.

        1 Reply Last reply Reply Quote 0
        • 1
          1337 @pmoncho
          last edited by 1337

          @pmoncho

          You can test how if-then statement works directly in powershell. Just type/copy it in straight on the command line,

          For example:

          • if ($false) { "True" } else { "False" }
          • if ("False") { "True" } else { "False" }
          • if ($true) { "True" } else { "False" }
          • if ("True") { "True" } else { "False" }
          • if ("") { "True" } else { "False" }
          • if (0) { "True" } else { "False" }
          • if (1) { "True" } else { "False" }

          You can also do the same test by forcing powershell to convert whatever you want into a boolean. And telling you if something is true or false.

          For example:

          • [bool]$false
          • [bool]"False"

          This is called type casting. But that belongs to more advanced programming concepts.

          pmonchoP 1 Reply Last reply Reply Quote 0
          • pmonchoP
            pmoncho @Danp
            last edited by

            @Danp said in Powershell "-eq" operator and "False":

            if ($UserE.enabled -eq "False") {

            Try if ($UserE.enabled -eq $False) {

            if ($UserE.enabled -ne "True") {...

            This works, but not in the way you think. It would likely give the incorrect result when enabled is $True.

            This worked. Greatly appreciate it.

            1 Reply Last reply Reply Quote 0
            • pmonchoP
              pmoncho @1337
              last edited by pmoncho

              @Pete-S said in Powershell "-eq" operator and "False":

              @pmoncho

              You can test how if-then statement works directly in powershell. Just type/copy it in straight on the command line,

              For example:

              • if ($false) { "True" } else { "False" }
              • if ("False") { "True" } else { "False" }
              • if ($true) { "True" } else { "False" }
              • if ("True") { "True" } else { "False" }
              • if ("") { "True" } else { "False" }
              • if (0) { "True" } else { "False" }
              • if (1) { "True" } else { "False" }

              You can also do the same test by forcing powershell to convert whatever you want into a boolean. And telling you if something is true or false.

              For example:

              • [bool]$false
              • [bool]"False"

              This is called type casting. But that belongs to more advanced programming concepts.

              Thanks for the excellent explanation. Now I just have to try an remember this. 🙂

              1 1 Reply Last reply Reply Quote 0
              • 1
                1337 @pmoncho
                last edited by 1337

                @pmoncho said in Powershell "-eq" operator and "False":

                Thanks for the excellent explanation. Now I just have to try an remember this. 🙂

                Yes, there is always that.

                Well, you can take comfort in the fact that all programming languages works about the same in this regard. So the knowledge is not wasted just on powershell.

                If you get down to what the CPU is actually doing, all if-then-else statements boils down to a comparison with zero that decides what code to execute.

                So the reason why false and true is really represented by zero and non-zero has a deeply technical reason.

                1 Reply Last reply Reply Quote 1
                • ObsolesceO
                  Obsolesce @pmoncho
                  last edited by

                  @pmoncho said in Powershell "-eq" operator and "False":

                  Trying to figure out why this will not work? I'm stumped

                  $UserID = read-host "UserID to disable"
                  $UserE = (Get-ADUser $UserID)
                  
                  write-host "Account Enabled?" $UserE.Enabled
                  
                  if ($UserE.enabled -eq "False") {
                     $a = read-host "Move to Disabled Accounts OU? (Y/N)"
                     $answer
                  }
                  

                  UserID to disable: test1
                  Account Enabled? False

                  C:\windows\system32

                  If I use the following all is works whether "Enabled" is True or False

                  if ($UserE.enabled -ne "True") {...
                  

                  In PowerShell, typically if it's in double quotes, it's a string. That's what you were were checking for, is if a given string equals the word "False".... instead of the boolean true/false, as $true/$false.

                  To find out what type of output you're dealing with, you can always use the built-in getType() method. You'll notice the Name property of String or Boolean.

                  0e7107d6-5adf-4408-ad41-3bdb8b799e79-image.png

                  pmonchoP 1 Reply Last reply Reply Quote 0
                  • pmonchoP
                    pmoncho @Obsolesce
                    last edited by

                    @Obsolesce said in Powershell "-eq" operator and "False":

                    @pmoncho said in Powershell "-eq" operator and "False":

                    Trying to figure out why this will not work? I'm stumped

                    $UserID = read-host "UserID to disable"
                    $UserE = (Get-ADUser $UserID)
                    
                    write-host "Account Enabled?" $UserE.Enabled
                    
                    if ($UserE.enabled -eq "False") {
                       $a = read-host "Move to Disabled Accounts OU? (Y/N)"
                       $answer
                    }
                    

                    UserID to disable: test1
                    Account Enabled? False

                    C:\windows\system32

                    If I use the following all is works whether "Enabled" is True or False

                    if ($UserE.enabled -ne "True") {...
                    

                    In PowerShell, typically if it's in double quotes, it's a string. That's what you were were checking for, is if a given string equals the word "False".... instead of the boolean true/false, as $true/$false.

                    To find out what type of output you're dealing with, you can always use the built-in getType() method. You'll notice the Name property of String or Boolean.

                    Thanks, as this will be VERY helpful because it was a question burning in the back of my head.

                    1 1 Reply Last reply Reply Quote 0
                    • 1
                      1337 @pmoncho
                      last edited by 1337

                      @pmoncho said in Powershell "-eq" operator and "False":

                      @Obsolesce said in Powershell "-eq" operator and "False":

                      In PowerShell, typically if it's in double quotes, it's a string. That's what you were were checking for, is if a given string equals the word "False".... instead of the boolean true/false, as $true/$false.

                      To find out what type of output you're dealing with, you can always use the built-in getType() method. You'll notice the Name property of String or Boolean.

                      Thanks, as this will be VERY helpful because it was a question burning in the back of my head.

                      Have a look at the difference between strings in double quotes and single quotes as well.

                      Best practice is to encapsulate strings in single quotes everywhere and use double quotes only when needed.

                      The difference is that double quoted strings are evaluated and not static. That's good when you want it but can cause unexpected behavior when you don't.

                      For example:

                      PS> $i=1
                      
                      PS> echo '$i is a string' 
                      $i is a string
                      
                      PS> echo "$i is a string"
                      1 is a string
                      
                      
                      ObsolesceO 1 Reply Last reply Reply Quote 1
                      • ObsolesceO
                        Obsolesce @1337
                        last edited by

                        @Pete-S said in Powershell "-eq" operator and "False":

                        Have a look at the difference between strings in double quotes and single quotes as well.

                        Yes this is a case where one point can lead to another and before you know it, it's a book.

                        His original post had nothing to do with single quotes so I wanted to watch how far I took it. That's why I purposefully said typically, because unless you cast a type before the double quotes, it's a string. But in that case is quite clear what the type is because it's literally telling you in the brackets.

                        1 pmonchoP 2 Replies Last reply Reply Quote 2
                        • 1
                          1337 @Obsolesce
                          last edited by 1337

                          @Obsolesce said in Powershell "-eq" operator and "False":

                          Yes this is a case where one point can lead to another and before you know it, it's a book.

                          Yeah, I here you. Well, you're right that it's vastly more important to know what constitutes a string and what doesn't versus using single or double quotes.

                          I guess it probably takes at least a hundred hours of study to learn the very basics of programming - if you don't have prior experience. So yeah, the things you need to know would be enough to fill a book for sure.

                          1 Reply Last reply Reply Quote 1
                          • pmonchoP
                            pmoncho @Obsolesce
                            last edited by

                            @Obsolesce said in Powershell "-eq" operator and "False":

                            @Pete-S said in Powershell "-eq" operator and "False":

                            Have a look at the difference between strings in double quotes and single quotes as well.

                            Yes this is a case where one point can lead to another and before you know it, it's a book.

                            I know what you mean. I once asked my high school math teacher why 1/0 is zero. I wish I could remember but she filled up the entire chalkboard with the math to prove it. 🙂

                            @Pete-S also. I greatly appreciate the extra info. I am always confused by single vs double quotes and when to use them. I will take your advice and use single first.

                            ObsolesceO 1 Reply Last reply Reply Quote 0
                            • ObsolesceO
                              Obsolesce @pmoncho
                              last edited by

                              @pmoncho said in Powershell "-eq" operator and "False":

                              I am always confused by single vs double quotes and when to use them. I will take your advice and use single first.

                              Single quotes are literal, double quotes resolve variables, expressions, cmdlets, anything with a $ or $() in double quotes.

                              1 Reply Last reply Reply Quote 1
                              • 1 / 1
                              • First post
                                Last post