Tag Archives: If

PSMonday #29: November 14, 2016

Topic: If, If-Else, If-ElseIf II

Notice: This post is a part of the PowerShell Monday series — a group of quick and easy to read mini lessons that briefly cover beginning and intermediate PowerShell topics. As a PowerShell enthusiast, this seemed like a beneficial way to ensure those around me at work were consistently learning new things about Windows PowerShell. At some point, I decided I would share these posts here, as well. Here’s the PowerShell Monday Table of Contents.

Let’s start by discussing the fact that we can reverse the way an If statement works. Last week, we discussed that we take a specific action(s) when conditions result in true, such as in the below, conceptual example.

If (<condition>) {
    <action>
}

This can be changed for the times it’s necessary. In the next example, we’ll complete the action if the condition is false — if it’s not true. The Not logical operator negates whatever follows it. Only use the Not operator when it’s absolutely necessary. If it’s not necessary, then there’s no reason to make your conditionals any more difficult to think through.

If (-Not(<condition>)) {
    <action>
}

The below examples help show how the Not logical operator changes the condition, and therefore, the action. In the first below example, the If statement will output the word “If,” because $true evaluates to True.

If ($true) {'If'} Else {'Else'}
If

In the next example, we negate $true, making it $false, so “Else” will be written to the screen.

If (-Not($true)) {'If'} Else {'Else'}
Else

In the next example, it’ll write “If” again. You can actually walk backwards through the conditions, saying it aloud: “True, becomes False, becomes True. It’ll write If.” See if you can do that with the last two examples. While these may be helpful for learning, there likely won’t ever be a time you’re negating a Not logical operator.

If (-Not(-Not($true))) {'If'} Else {'Else'}
If

If (-Not(-Not(-Not($true)))) {'If'} Else {'Else'}

If (-Not(-Not(-Not(-Not(-Not(-Not($true))))))) {'If'} Else {'Else'}

In closing, we’ll take a look at a final example. On the first line in the below example, we assign the variable x a value of 9. When we only put $x inside the If statement’s condition, it only evaluates whether or not the variable x exists. It pays no attention to the value that’s been assign to the variable x. In the If statement following that one, we evaluate the value assigned to the variable x. If that value is equal to 9, and it is, we’ll write the word  “If” to the screen.

$x = 9

If ($x) {
    Write-Output -InputObject 'If'
}
If

If ($x -eq 9) {
    Write-Output -InputObject 'If'
}
If

We’ll close up the If topic next week. Please let me know if there are any questions on today’s PSMonday.

Checking a Variable for a Path

I helped someone on a forum recently that needed to determine if a path existed or not. Providing the path did exist, they would need to perform an action. The problem wasn’t what to do (in the end), it was determining if the path existed (to begin with). Here’s a rough draft of their code:

$Path = 'C:\Scripts\TestFolder'

If ($Path) {
    Remove-Item -Path $Path
}

If you’ve been doing this a little while, then it’s quite easy to spot the problem with the “logic” used in the example above. The If statement’s condition is only evaluating if $Path has a value (if it contains something). The fact that the value may, or may not, be a path, and whether or not it exists (if it is a path), is not being evaluated. I recommended they use the Test-Path cmdlet, to ensure the value stored in $Path, was a path, and that it existed.

$Path = 'C:\Scripts\TestFolder'

If (Test-Path -Path $Path) {
    Remove-Item -Path $Path
}

There are times when all you will want to know is if a variable contains a value. As an example, we can use a variable to record if a specific process is running (at the time the variable is created), and take action depending on if it is, or isn’t.

The example below checks for a process called pn (Programmer’s Notepad) and stores the results in the $PNProcess variable. We can then check and determine if $PNProcess has been set, or assigned a value, and make a determination of what to do based on the results. Since we’re only looking to see if our variable contains a value, then this is a perfectly suitable solution.

$PNProcess = Get-Process -ProcessName pn -ErrorAction SilentlyContinue

If ($PNProcess) {
    Write-Output -Verbose "Programmer's Notepad is running."
} Else {
    Write-Output -Verbose "Programmer's Notepad is not running."
}

Note: While I would normally wrap a string in single quotes, double quotes are being used because single quotes will not work when there’s a single quote in the string.