# $true or $false in powershell function

**URL:** <https://forums.powershell.org/t/true-or-false-in-powershell-function/15384>\
**Category:** PowerShell Help\
**Created:** [December 14, 2020, 2:04am UTC](https://forums.powershell.org/t/true-or-false-in-powershell-function/15384 "2020-12-14T02:04:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianj](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@adrianj](https://forums.powershell.org/u/adrianj)\
**Post date:** [December 14, 2020, 2:04am UTC](https://forums.powershell.org/t/true-or-false-in-powershell-function/15384/1 "2020-12-14T02:04:58Z")

</div>

I came across one script and I’m puzzled about one simple function in a script. To my understanding, result should be always $false as it has return $false at the bottom of the script or I’m missing something? Can someone explains the logic here. Thank you

#`This function checks to see if the file should be ignored.`  
`Function Is_Temporary`  
`{`  
`$TheFile = "filename.txt"`

`# Check that the full path is long enough to be applicable, then ask if the file's last few characters match a temporary extension.`  
`$Temp_Files | ForEach {If ($Object.length -ge $.length -AND $Object.substring($Object.length - $.length, $.length) -eq $) {Return $True}}`

`If ($TheFile.substring(0,1) -eq "~") {Return $True}`  
`If ($TheFile -eq "thumbs.db") {Return $True}`  
`If ($TheFile.length -ge 4 -AND $TheFile.substring(0,4) -eq ".dat") {Return $True}`

`Return $False`  
`}`

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [December 14, 2020, 3:32am UTC](https://forums.powershell.org/t/true-or-false-in-powershell-function/15384/2 "2020-12-14T03:32:55Z")

</div>

Is this the entire function? I don’t see any parameters and the function references variables that are not defined ($Temp\_Files). I reformatted your code so I can reference line numbers in my explanation.

```
# This function checks to see if the file should be ignored.
Function Is_Temporary
{
    $TheFile = "filename.txt"

    # Check that the full path is long enough to be applicable, then ask if the file's last few characters match a temporary extension.
    $Temp_Files | ForEach {
        If ($Object.length -ge $_.length -AND $Object.substring($Object.length - $_.length, $_.length) -eq $_) {Return $True}}

    If ($TheFile.substring(0,1) -eq "~") {Return $True}
    If ($TheFile -eq "thumbs.db") {Return $True}
    If ($TheFile.length -ge 4 -AND $TheFile.substring(0,4) -eq ".dat") {Return $True}

    Return $False
}
```

Line 4 - $TheFile is assigned to the string “filename.txt” I don’t know why

Line 7 - $Temp\_Files is never assigned so doesn’t exist which means the ForEach loop will not happen.

Line 10 - False because “filename.txt” does not start with “~”

Line 11 - False because “filename.txt” is not “thumbs.db”

Line12 - First part of logic statement is True because “filename.txt”.length is greater than 4, but second part is False because “filename.txt”.substring(0,4) is “file”

Line 14 will return False

Anytime a function hits the keyword “Return” it will return that object and stop execution. That means if any of the conditions on lines 8, 10, 11, or 12 would have evaluated to True then the function would have returned True.

&nbsp;

---

<div class="post-metadata">

**Author:** ![adrianj](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@adrianj](https://forums.powershell.org/u/adrianj)\
**Post date:** [December 14, 2020, 5:29am UTC](https://forums.powershell.org/t/true-or-false-in-powershell-function/15384/3 "2020-12-14T05:29:15Z")

</div>

Hi Mike. Thank you for your reply.

It is just one simple function within the script.

$thefile actually calls another function but I removed that for simplicity.

$temp\_file is a variable outside of function.

But your last statement resolved my dilemma. I thought the same that it returns first $true but when I tested with “TheFile” variable (missed object variable"), the result was always $false and I was convinced that it always returns $false as it is the last return in script.

Thank you so much again for your explanation 🙂

&nbsp;

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [December 14, 2020, 5:33am UTC](https://forums.powershell.org/t/true-or-false-in-powershell-function/15384/4 "2020-12-14T05:33:59Z")

</div>

[quote quote=279507]Hi Mike. Thank you for your reply.

It is just one simple function within the script.

$thefile actually calls another function but I removed that for simplicity.

$temp\_file is a variable outside of function.

But your last statement resolved my dilemma. I thought the same that it returns first $true but when I tested with “TheFile” variable (missed object variable”), the result was always $false and I was convinced that it always returns $false as it is the last return in script.

Thank you so much again for your explanation 🙂

[/quote]

You can’t reference a variable defined outside of the function. It is out of scope so regardless of how it was previously designed, inside the function definition it will be $null. If you need the value, create a parameter and pass it in as an argument.

---

<div class="post-metadata">

**Author:** ![adrianj](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@adrianj](https://forums.powershell.org/u/adrianj)\
**Post date:** [December 14, 2020, 6:56am UTC](https://forums.powershell.org/t/true-or-false-in-powershell-function/15384/5 "2020-12-14T06:56:31Z")

</div>

It is a script scope variable so that is fine. Thanks again

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/true-or-false-in-powershell-function/15384/6 "2024-05-16T20:28:10Z")

</div>


