EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell If-Not
Secondary Sidebar
PowerShell Tutorial
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

PowerShell If-Not

By Priya PedamkarPriya Pedamkar

PowerShell If-Not

Introduction to PowerShell If-Not

The following article provides an outline on PowerShell If-Not. In most of the scripts, it is necessary to make some comparison and take some decisions based on the outcome of comparison. This is known as conditional making and this is achieved in PowerShell with the help of conditional operators. Conditional Operators are used to compare two or more values or conditions and based on those condition appropriate action is performed. If condition is one type of conditional operator. The if operator has a statement or value that needs to be evaluated and based on the result and the corresponding action is executed.

The if statement generally has one or multiple else statements followed by it. If the evaluation of condition is true, the code inside if block is executed else the code in the else part is executed. In case of If-Not it is nothing but negating the If block. This means if the condition is not met then if part is executed and if the condition if is met else part if is executed. This is also known as reversal of if operator. Here we will see various if conditional operator and If-Not operator.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax of If-Not Operator

The -Not operator is used to change an expression result from false to true or true to false. The alias of -not is!.

The syntax is as follows:

If(-not(statement)){}

Or

If(!(statement)){}

Or

If(-not($test)){}

Or

If(!($test)){}

Example:

Code:

Write-Host "IF-Not example"
if( -not(1 -lt 2))
{
Write-Host "If condition is executed"
}
else
{
Write-Host "else condition is executed"
}

Output:

else condition is executed

  • In the above example, the if condition checks first if 1 is lesser than 2. It is true.
  • Then the not operator makes it false.
  • So, the control is shifted to else statement and that block is executed.
  • In the above if the condition if changed as 3 from 1 then, the if condition will become false and after not it will become true.

Then if statement will be executed as below.

Code:

Write-Host "IF-Not example"
if( -not(3 -lt 2))
{
Write-Host "If condition is executed"
}
else
{
Write-Host "else condition is executed"
}

Output:

if condition is executed

Examples of PowerShell If-Not

Given below are the examples:

Example #1

Code:

Write-Host "Welcome to demo of IF-Not Operator"
Write-Host "This is will ask the user for his age and weight and let him know his bmi status"
$age=Read-Host "Enter your age"
$weight= Read-Host "Enter your weight"
if(-not($age -gt 40 -and $AGE -LT 50) -and $weight -le 50)
{
Write-Host "Your BMI Level is normal"
Write-Host "You are safe"
}
ElseIf(-not($age -gt 40 -and $AGE -LT 50) -and ($weight -Gt 50 -and $weight -le 60))
{
Write-Host "Your BMI Level is CLOSE TO normal"
Write-Host "You are safe, but please take care"
}
ElseIf(($age -gt 40 -and $AGE -LT 50) -and ($weight -Gt 60 -and $weight -le 70))
{
Write-Host "Your BMI Level is NOT normal"
Write-Host "You need to exercise"
}
Elseif(($age -gt 50 -and $AGE -LT 60) -and $weight -le 50)
{
Write-Host "Your BMI Level is normal"
Write-Host "You are safe"
}
ElseIf(($age -gt 50 -and $AGE -LT 60) -and ($weight -Gt 50 -and $weight -le 60))
{
Write-Host "Your BMI Level is CLOSE TO normal"
Write-Host "You are safe, but need to monitor"
}
ElseIf(($age -gt 50 -and $AGE -LT 60) -and ($weight -Gt 60 -and $weight -le 70))
{
Write-Host "Your BMI Level is absolutely normal"
Write-Host "You are safe, please keep up the good work"
}
Elseif(($age -gt 60 -and $AGE -LT 70) -and $weight -le 50)
{
Write-Host "Your BMI Level is not normal"
Write-Host "You are not safe. Please consult doctor"
}
ElseIf(($age -gt 60 -and $AGE -LT 70) -and ($weight -Gt 50 -and $weight -le 60))
{
Write-Host "Your BMI Level is perfect"
Write-Host "Keep up the good work"
}
ElseIf(-not($age -gt 60 -and $AGE -LT 70) -and ($weight -Gt 60 -and $weight -le 70))
{
Write-Host "Your BMI Level is too much"
Write-Host "You need to exercise"
}
else
{
Write-Host "Please enter appropriarte age and weight"
}

Output:

powershell if not 3

Example #2

Code:

Write-Host "Welcome to demo of various if operations with different operators"
Write-Host "Simple if statement"
$no1=Read-Host "Enter First number"
$no2= Read-Host "Enter second number"
if($no1 -gt $no2)
{
Write-Host "Number entered first" $no1 "is greater"
}
else
{
Write-Host "Number entered second" $no2 "is greater"
}
$number=Read-Host "Enter a three digit number"
if(($number -ge 100) -and ($number -lt 300) -and ($number.Length -lt 4))
{
Write-Host "Entered number is in the range 100 to 299"
}
elseif(($number -ge 300) -and ($number -lt 700) -and ($number.Length -lt 4))
{
Write-Host "Entered number is in the range 300 to 700"
}
elseif(($number -ge 700) -and ($number -lt 999) -and ($number.Length -lt 4))
{
if($number.Length -eq 3)
{
Write-Host "Entered number is in the range 700 to 999"
}
else
{
Write-Host "Not a three digit number"
}
}
else
{
Write-Host "Not a three digit number"
}
Write-Host "If not demo using !"
$username= Read-Host "Enter a user name"
if(!$username)
{
Write-Host "username is empty"
}
else
{
Write-Host "Entered user name is" $username
}

Output:

powershell if not 4

Example #3

Code:

Write-Host "If not Demo"
[email protected]("viki","ravi","raju")
$input=Read-Host "Enter a name"
if(-not($array -contains $input))
{
Write-Host "Entered input is not  present in the array"
}
else
{
Write-Host "Entered input is  present in the array"
}

Output:

powershell if not 5

Conclusion

Thus, the article covered in detail about the If-Not conditional operator in PowerShell. It showed various examples on how to use the if conditional operator with various comparison checks. The article also covered how various scenarios can be checked using the if loop and multiple if else statements. To learn more about the if statement and various comparison operators with if statement it would be advisable to write sample scripts and practice them.

Recommended Articles

This is a guide to PowerShell If-Not. Here we discuss the introduction to PowerShell If-Not along with respective examples for easy and better understanding. You may also have a look at the following articles to learn more –

  1. PowerShell Convert to String
  2. PowerShell Concatenate String
  3. Useful PowerShell Scripts
  4. PowerShell Wildcards | Types
  5. PowerShell Rename Folder (Examples)
  6. Examples of PowerShell Send Mail
  7. PowerShell Get-Service | Examples
  8. Guide to PowerShell Continue
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more