Contact Info

Crumbtrail » Administration » Powershell » Powershell 1.0 » if

if - Powershell 1.0 CmdLet

Microsoft Windows PowerShell is a command-line shell and scripting tool based on the Microsoft .NET Framework. It is designed for system administrators, engineers and developers to control and automate the administration of Windows and applications.

More than hundred command-line tools (so called "cmdlets") can be used to perform system administration tasks and Windows Management Instrumentation (WMI). These cmdlets are easy to use, with standard naming conventions and common parameters, and standard tools for piping, sorting, filtering, and formatting data and objects.


Conditionally perform a command


Condition  An expression that will evaluate to true or false,
              often utilising one or more comparison operators.

              A powershell or external command to run if the condition is true.

Replace the text in the variable $MyDemoVar:

PS C:\>if ($MyDemoVar -like "*SS64*") {$MyDemoVar -replace "SS64", "Demonstration Example"}

Print the running services in green and stopped services in red:

PS C:\>get-service | foreach-object{ if ($_.status -eq "stopped") {write-host -f red $ $_.status}` else{ write-host -f green $ $_.status}}