SlideShare a Scribd company logo
Windows PowerShell Basics - IF & Else Conditional Statements
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To..................................................................................................................................................1
Pre-Requisites ...........................................................................................................................................1
PowerShell Script – Function ........................................................................................................................1
Code Snippet – IF Statement ....................................................................................................................1
PowerShell Output – IF Condition.........................................................................................................2
PowerShell Output – IF Condition – No Input.......................................................................................2
Code Snippet – IF & ELSE Statement ........................................................................................................2
PowerShell Output – IF & ELSE Condition.............................................................................................2
PowerShell Output – IF & ELSE Condition – No Input...........................................................................3
Windows PowerShell Basics - IF & Else Conditional Statements
1 | P a g e
Overview
PowerShell functions are helpful in saving time when you have repetitive task(s) to be executed. In this
guide we will demonstrate, as to how to construct a “IF” & “ELSE” conditional statements.
Applies To
Tested on Windows 10, Windows 2008 R2 and Windows 2012.
Pre-Requisites
Launch PowerShell Command Console or PowerShell ISE.
To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or
“Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.
Each Policy type and its purpose is shown in the below table.
Policy Type Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.
PowerShell Script – Function
This PowerShell script call function with function’s mandatory argument / parameter value, when the
argument value is passed; Passed value will be displayed else an exception will be thrown.
Code Snippet – IF Statement
The code snippet is for demonstrating “IF” Statement / condition only.
#
# Simple Conditional Statements - IF
#
Clear-Host
$Input=Read-Host "Key-In Input"
if ($Input -ne "") {
Write-Host "`nValid Input, Keyed Input String:" $Input -ForegroundColor Green
[console]::Beep(500,600)
}
Windows PowerShell Basics - IF & Else Conditional Statements
2 | P a g e
PowerShell Output – IF Condition
When Input text is provided upon execution of the script;
PowerShell Output – IF Condition – No Input
When “NO” Input text is provided upon execution of the script; script will simply execute and exit.
Code Snippet – IF & ELSE Statement
The code snippet below is for demonstrating “IF” and “ELSE” statement.
#
# Simple Conditional Statements - IF & Else Statement
#
Clear-Host
$Input=Read-Host "Key-In Input"
if ($Input -ne "") {
Write-Host "`nValid Input, Keyed Input String:" $Input -ForegroundColor Green
[console]::Beep(500,600)
} else {
Write-Host "Invalid Input" -ForegroundColor Red
[console]::Beep(900,600)
}
PowerShell Output – IF & ELSE Condition
When Input text is provided upon execution of the script;
Windows PowerShell Basics - IF & Else Conditional Statements
3 | P a g e
PowerShell Output – IF & ELSE Condition – No Input
When “NO” Input text is provided upon execution of the script; script will simply execute and message
will be displayed as shown below.

More Related Content

PDF
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
PDF
How To Create Power Shell Function Mandatory Parameter Value
PDF
Windows PowerShell Basics - How To List PSDrive Info
PDF
How To Disable IE Enhanced Security Windows PowerShell
PDF
Windows PowerShell Basics – How To Create powershell for loop
PDF
How To Check IE Enhanced Security Is Enabled Windows PowerShell
PDF
How To Check file exists and Delete PowerShell
PDF
Install and Configure RSyslog – CentOS 7 / RHEL 7
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
How To Create Power Shell Function Mandatory Parameter Value
Windows PowerShell Basics - How To List PSDrive Info
How To Disable IE Enhanced Security Windows PowerShell
Windows PowerShell Basics – How To Create powershell for loop
How To Check IE Enhanced Security Is Enabled Windows PowerShell
How To Check file exists and Delete PowerShell
Install and Configure RSyslog – CentOS 7 / RHEL 7

What's hot (20)

PDF
How To Connect to Active Directory User Validation
PDF
Install Active Directory PowerShell Module on Windows 10
PDF
How To Install and Configure Open SSH Server on Ubuntu
PPTX
psCloudstack Internals
PDF
How To View Current Execution Policy PowerShell
PDF
Bash Script - How To Monitor Application Error Logs and Send Notification
PDF
How To Create EBS Snapshot and Restore EBS Snapshot – Linux Instance
PDF
How To Install and Configure SNMP on RHEL 7 or CentOS 7
PDF
How To Manage Linux User on RHEL 7
PDF
How To Install and Configure Apache SSL on CentOS 7
PDF
How To Install and Configure AWS CLI for Windows
PDF
How To Configure Amazon EC2 Security Groups
ODP
Specs Presentation
PPTX
Salesforce Integration using REST SOAP and HTTP callouts
PDF
Nginx bind() to 0.0.0.0:9080 failed
DOCX
How to configure amazon ec2 load balancer
PDF
How to Install and Configure Cacti on Linux
PDF
Install and Configure WordPress in AWS on RHEL 7 or CentOS 7
PPTX
PowerShell: Automation for everyone
PDF
Basic commands for powershell : Configuring Windows PowerShell and working wi...
How To Connect to Active Directory User Validation
Install Active Directory PowerShell Module on Windows 10
How To Install and Configure Open SSH Server on Ubuntu
psCloudstack Internals
How To View Current Execution Policy PowerShell
Bash Script - How To Monitor Application Error Logs and Send Notification
How To Create EBS Snapshot and Restore EBS Snapshot – Linux Instance
How To Install and Configure SNMP on RHEL 7 or CentOS 7
How To Manage Linux User on RHEL 7
How To Install and Configure Apache SSL on CentOS 7
How To Install and Configure AWS CLI for Windows
How To Configure Amazon EC2 Security Groups
Specs Presentation
Salesforce Integration using REST SOAP and HTTP callouts
Nginx bind() to 0.0.0.0:9080 failed
How to configure amazon ec2 load balancer
How to Install and Configure Cacti on Linux
Install and Configure WordPress in AWS on RHEL 7 or CentOS 7
PowerShell: Automation for everyone
Basic commands for powershell : Configuring Windows PowerShell and working wi...
Ad

Similar to How To Construct IF and Else Conditional Statements (20)

PDF
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
PPTX
Power shell voor developers
PPTX
Power shell training
PDF
powershell.pdf
PPTX
Unix shell scripts
PPTX
Licão 11 decision making - statement
PDF
Powershell training material
PDF
How To Create PowerShell Function
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
PDF
Practical PowerShell Programming for Professional People
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
PPTX
Powershell Demo Presentation
PPT
Advanced linux chapter ix-shell script
PPT
PowerShell Scripting and Modularization (TechMentor Fall 2011)
PPTX
Power shell basics day 2
PPTX
Power shell basics day 6
PDF
Powershell notes
PPTX
AOS_Module_3ssssssssssssssssssssssssssss.pptx
PPTX
PowerShell Zero To Hero Workshop!
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
Power shell voor developers
Power shell training
powershell.pdf
Unix shell scripts
Licão 11 decision making - statement
Powershell training material
How To Create PowerShell Function
Windows Powershell in Action 1st Edition Bruce G. Payette
Windows Powershell in Action 1st Edition Bruce G. Payette
Practical PowerShell Programming for Professional People
Windows Powershell in Action 1st Edition Bruce G. Payette
Powershell Demo Presentation
Advanced linux chapter ix-shell script
PowerShell Scripting and Modularization (TechMentor Fall 2011)
Power shell basics day 2
Power shell basics day 6
Powershell notes
AOS_Module_3ssssssssssssssssssssssssssss.pptx
PowerShell Zero To Hero Workshop!
Ad

More from VCP Muthukrishna (18)

PDF
How to Fix Duplicate Packages in YUM on CentOS 7
PDF
How To Install and Configure GNome on CentOS 7
PDF
How To Connect To Active Directory PowerShell
PDF
How To List Files on Remote Server - PowerShell
PDF
How To List Files and Display In HTML Format
PDF
How To Check and Delete a File via PowerShell
PDF
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
PDF
How To Setup SSH Keys on CentOS 7
PDF
How To List Nginx Modules Installed / Complied on CentOS 7
PDF
How To Configure Nginx Load Balancer on CentOS 7
PDF
How To Install and Configure Screen on CentOS 7
PDF
How To Install and Configure Salt Master on Ubuntu
PDF
How To Protect SSH Access with Fail2Ban on RHEL 7
PDF
How To Configure SNMP Logging on RHEL 7
PDF
How To Find Package Installation Date on RHEL 7
PDF
How to Upgrade Openfire on CentOS 7
PDF
How To Reset root Password on CentOS 7
PDF
How To Install and Use ABRT CLI on RHEL 7
How to Fix Duplicate Packages in YUM on CentOS 7
How To Install and Configure GNome on CentOS 7
How To Connect To Active Directory PowerShell
How To List Files on Remote Server - PowerShell
How To List Files and Display In HTML Format
How To Check and Delete a File via PowerShell
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
How To Setup SSH Keys on CentOS 7
How To List Nginx Modules Installed / Complied on CentOS 7
How To Configure Nginx Load Balancer on CentOS 7
How To Install and Configure Screen on CentOS 7
How To Install and Configure Salt Master on Ubuntu
How To Protect SSH Access with Fail2Ban on RHEL 7
How To Configure SNMP Logging on RHEL 7
How To Find Package Installation Date on RHEL 7
How to Upgrade Openfire on CentOS 7
How To Reset root Password on CentOS 7
How To Install and Use ABRT CLI on RHEL 7

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Spectroscopy.pptx food analysis technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Approach and Philosophy of On baking technology
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
The AUB Centre for AI in Media Proposal.docx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Programs and apps: productivity, graphics, security and other tools
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Encapsulation_ Review paper, used for researhc scholars
Digital-Transformation-Roadmap-for-Companies.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Spectroscopy.pptx food analysis technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Understanding_Digital_Forensics_Presentation.pptx
Approach and Philosophy of On baking technology

How To Construct IF and Else Conditional Statements

  • 1. Windows PowerShell Basics - IF & Else Conditional Statements i | P a g e Table of Contents Overview.......................................................................................................................................................1 Applies To..................................................................................................................................................1 Pre-Requisites ...........................................................................................................................................1 PowerShell Script – Function ........................................................................................................................1 Code Snippet – IF Statement ....................................................................................................................1 PowerShell Output – IF Condition.........................................................................................................2 PowerShell Output – IF Condition – No Input.......................................................................................2 Code Snippet – IF & ELSE Statement ........................................................................................................2 PowerShell Output – IF & ELSE Condition.............................................................................................2 PowerShell Output – IF & ELSE Condition – No Input...........................................................................3
  • 2. Windows PowerShell Basics - IF & Else Conditional Statements 1 | P a g e Overview PowerShell functions are helpful in saving time when you have repetitive task(s) to be executed. In this guide we will demonstrate, as to how to construct a “IF” & “ELSE” conditional statements. Applies To Tested on Windows 10, Windows 2008 R2 and Windows 2012. Pre-Requisites Launch PowerShell Command Console or PowerShell ISE. To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”. Each Policy type and its purpose is shown in the below table. Policy Type Purpose Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode. AllSigned Only scripts signed by a trusted publisher can be run. RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run. Unrestricted No restrictions; all Windows PowerShell scripts can be run. PowerShell Script – Function This PowerShell script call function with function’s mandatory argument / parameter value, when the argument value is passed; Passed value will be displayed else an exception will be thrown. Code Snippet – IF Statement The code snippet is for demonstrating “IF” Statement / condition only. # # Simple Conditional Statements - IF # Clear-Host $Input=Read-Host "Key-In Input" if ($Input -ne "") { Write-Host "`nValid Input, Keyed Input String:" $Input -ForegroundColor Green [console]::Beep(500,600) }
  • 3. Windows PowerShell Basics - IF & Else Conditional Statements 2 | P a g e PowerShell Output – IF Condition When Input text is provided upon execution of the script; PowerShell Output – IF Condition – No Input When “NO” Input text is provided upon execution of the script; script will simply execute and exit. Code Snippet – IF & ELSE Statement The code snippet below is for demonstrating “IF” and “ELSE” statement. # # Simple Conditional Statements - IF & Else Statement # Clear-Host $Input=Read-Host "Key-In Input" if ($Input -ne "") { Write-Host "`nValid Input, Keyed Input String:" $Input -ForegroundColor Green [console]::Beep(500,600) } else { Write-Host "Invalid Input" -ForegroundColor Red [console]::Beep(900,600) } PowerShell Output – IF & ELSE Condition When Input text is provided upon execution of the script;
  • 4. Windows PowerShell Basics - IF & Else Conditional Statements 3 | P a g e PowerShell Output – IF & ELSE Condition – No Input When “NO” Input text is provided upon execution of the script; script will simply execute and message will be displayed as shown below.