Share this page : facebooktwitterlinkedinmailfacebooktwitterlinkedinmail
Construction of the PowerShell ‘If’ Statement

As with so many PowerShell constructions, the type of bracket signifies how to break the script into sections.  It’s worth tattooing into our memory that (parenthesis brackets are for the first part, namely the condition), while {braces are for the block command}.

If (condition) {Do stuff}
 # Another explanation would be
 If (test) {
 "Execute when true"
 }

Summary:  In PowerShell, ‘If’ executes a statements conditional on the truth of the test expression.

 

Example:

@echo off
REM check the if the file with keyword Cute in the filename
IF EXIST "B:\Tools\*Cute*.<" (
 ECHO "file exist"
) ELSE (
 ECHO "file not exist"
)