# If Statement

**URL:** <https://forums.powershell.org/t/if-statement/11098>\
**Category:** PowerShell Help\
**Created:** [August 8, 2018, 5:08am UTC](https://forums.powershell.org/t/if-statement/11098 "2018-08-08T05:08:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [August 8, 2018, 5:08am UTC](https://forums.powershell.org/t/if-statement/11098/1 "2018-08-08T05:08:58Z")

</div>

After some help, i have a bit of a complicated If Statement

```
$fN = $Firstname.text

$ln = $Lastname.Text

$CN = $CompanyName.Text

$Email = $Emailaddress.text
```

FN - Cannot be Null or empty

LN - Cannot be Null or empty

CN - Cannot be Null or empty

Email - cannot be null or empty and cannot contain spaces.

if any or all of those are present then stop, other wise do stuff

&nbsp;

&nbsp;

in all honesty im a bit lost with it ☹

&nbsp;

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [August 8, 2018, 5:54am UTC](https://forums.powershell.org/t/if-statement/11098/2 "2018-08-08T05:54:40Z")

</div>

You can use a foreach loop or switch statement. Example is below.

```
$fN = 'firstname'
$ln = 'lastname'
$CN = 'yourcompanyname'
$Email = 'youremail'
$all = $fn,$ln,$cn,$Email

foreach ($a in $all){
If ($a){"Stop Something"}Else{"Do Something"}
}
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 8, 2018, 6:08am UTC](https://forums.powershell.org/t/if-statement/11098/3 "2018-08-08T06:08:24Z")

</div>

Another approach could be this

```
IF( -not [string]::IsNullOrEmpty($fN) -and 
    -not [string]::IsNullOrEmpty($ln) -and 
    -not [string]::IsNullOrEmpty($CN) -and 
    -not [string]::IsNullOrEmpty($Email) -and
    $email -notmatch '\s' 
){
    'cool'
}
Else {
    'uncool'
}
```

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [August 8, 2018, 6:43am UTC](https://forums.powershell.org/t/if-statement/11098/4 "2018-08-08T06:43:08Z")

</div>

I’d wrap this in a function that takes these values as input. Then, you can apply parameter validation to stop it running if a value isn’t supplied:

```
function Do-Thing {
  param(
    [ValidateNotNullOrEmpty()]
    $FirstName,

    [ValidateNotNullOrEmpty()]
    $LastName,

    [ValidateNotNullOrEmpty()]
    $Company,

    [ValidateNotNullOrEmpty()]
    $EmailAddress,
  )
  # Do things here
}
$Params = @{
  Firstname = $Firstname.Text
  LastName = $Lastname.Text
  Company = $CompanyName.Text
  EmailAddress = $EmailAddress.Text
}
Do-Thing @Params
```

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [August 8, 2018, 6:56am UTC](https://forums.powershell.org/t/if-statement/11098/5 "2018-08-08T06:56:41Z")

</div>

Hugely Appreciated guys

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [August 8, 2018, 7:02am UTC](https://forums.powershell.org/t/if-statement/11098/6 "2018-08-08T07:02:13Z")

</div>

IMO, Joel’s example is a wise approach.

Same function with check for white spaces as well.

```
function Do-Thing {
  param(
    [ValidateNotNullOrEmpty()]
    $FirstName,

    [ValidateNotNullOrEmpty()]
    $LastName,

    [ValidateNotNullOrEmpty()]
    $Company,

    [ValidateNotNullOrEmpty()]
    [Validatepattern('^\S*$')]
    $EmailAddress,
  )
  # Do things here
}
```

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [August 8, 2018, 7:16am UTC](https://forums.powershell.org/t/if-statement/11098/7 "2018-08-08T07:16:37Z")

</div>

Aww, I thought the css colored text would work (copy and paste from vscode). And these windows are quite narrow (62 characters). Anyway, here’s my version, with all my tricks to bear.

```
#23456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 

if ($fN -and $ln -and $CN -and $Email -replace ' ') {
  'all good'
}
```

---

<div class="post-metadata">

**Author:** ![riedyw](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/riedyw/32/53_2.png) [@riedyw](https://forums.powershell.org/u/riedyw)\
**Post date:** [August 30, 2018, 4:16pm UTC](https://forums.powershell.org/t/if-statement/11098/8 "2018-08-30T16:16:22Z")

</div>

I hate to be a stickler, but an email address can have a space in it. What will happen is that the space is read as a delimiter between the DisplayName and the other components of the email address (User, Host, Address)

My suggestion is to use the function I wrote Test-IsValidEmailAddress taken from inspiration I found on the Internet with a whole lot more logic and comment based help.

```
Function Test-IsValidEmailAddress {

    [CmdletBinding()]
    [Outputtype([bool])]
    Param (
        [parameter(Mandatory=$True,Position=0,ValueFromPipeLine=$True,ValueFromPipeLineByPropertyName=$True)]
        [Alias("Address")]
        [string] $EmailAddress
    )
    Process {
        Write-Verbose -message "You entered email address: [$($EmailAddress)]"
        Try {
            $temp = [System.Net.Mail.MailAddress] $EmailAddress
            write-verbose -message "Address resolved to: [$($temp.Address)]"
            if ($temp.Address -ne $EmailAddress) {
                write-verbose -message "[$($temp.Address)] does not match [$($EmailAddress)]"
                write-ouput $false
            } else {
                Write-Verbose -message "Address valid, no guarantee that address [$($EmailAddress)] exists."
                Write-Output $True
            }
        } Catch {
            Write-Verbose -message "The address is NOT valid."
            Write-Output -inputobject $False
        }
    }
} #EndFunction Test-IsValidEmailAddress
```

---

<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:34pm UTC](https://forums.powershell.org/t/if-statement/11098/9 "2024-05-16T20:34:28Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


