hello
Google
Welcome to Carpe Diem: Flaphead@Home Sign in | Join | Help

Carpe Diem: Flaphead.com

Seize the Day

News


  • Add to Technorati Favorites <script type="text/javascript" src="http://technorati.com/embed/3ni3q36ikc.js"> </script>
    This information is provided "AS IS" with no warranties, and confers no rights. Also some of the information contains my views and thoughts.
    <script src="http://widgets.technorati.com/t.js" type="text/javascript" charset="UTF-8"></script>

    Add Me! - Search Engine Optimization

    I heart FeedBurner

Powershell 101: IF .. Else

Okay as a beginner this took a bit of time to perfect so here goes. For some of you hardened programmers this might be a bit basic, but I had to start somewhere.  My background is cmd and vbs files with some html chucked in, so this syntax is new to me

Simple IF, Else

$a = "Powershell"
IF ($a -eq "PowerShell")
{
 "Statement is True"
}
ELSE
{
 "Statement is False"
}

You can condence this to:

$a = "Powershell"
IF ($a -eq "PowerShell"){ "Statement is True"} ELSE { "Statement is False"}

Now if you want to check for a NULL then it gets fun

$b = “Hello”
IF ($b -eq $NULL)
{  
"B is NULL"
}
ELSE
{
 "B is NOT NULL"
}


So you have whole load of conditional statement like these:

Operator

Definition

-lt

Less than

-le

Less than or equal to

-gt

Greater than

-ge

Greater than or equal to

-eq

Equal to.  If the left hand side of the operator is an array and the right hand side is a scalar, the equivalent values of the left hand side will be returned

-ne

Not Equal to. If the left hand side of the operator is an array and the right hand side is a scalar, the not equivalent values of the left hand side will be returned

-contains

Determine elements in a group, this always returns Boolean $True or $False.

-notcontains

Determine excluded elements in a group, this always returns Boolean $True or $False.

-like

Like - uses wildcards for pattern matching

-notlike

Not Like - uses wildcards for pattern matching

-match

Match - uses regular expressions for pattern matching

-notmatch

Not Match - uses regular expressions for pattern matching

-band

Bitwise AND

-bor

Bitwise OR

-is

Is of Type

-isnot

Is not of Type

These operators are the case-sensitive counterparts:

Operator

Definition

-clt

Less than (case sensitive)

-cle

Less than or equal to (case sensitive)

-cgt

Greater than (case sensitive)

-cge

Greater than or equal to (case sensitive)

-ceq

Equal to  (case sensitive)

-cne

Not Equal to (case sensitive)

-clike

Like (case sensitive)

-cnotlike

Not Like (case sensitive)

-ccontains

left hand side contains right hand side in a case sensitive manner

-cnotcontains

determine excluded elements in a group in a case sensitive manner

-cmatch

Match (case sensitive)

-cnotmatch

Not Match (case sensitive)

Other operators:

Operator

Definition

+

Add

-

Subtract

*

Multiply

/

Divide

%

Modulo

-not

logical not

!

logical not

-band

binary and

-bor

binary or

-bnot

binary not

-replace

Replace (e.g.  "abcde" –replace "b","B") (case insensitive)

-ireplace

Case-insensitive replace (e.g.  "abcde" –ireplace "B","3")

-creplace

Case-sensitive replace (e.g.  "abcde" –creplace "B","3")

-and

AND (e.g. ($a -ge 5 -AND $a -le 15) )

-or

OR  (e.g. ($a –eq "A" –OR $a –eq "B") )

-is

IS type (e.g. $a -is [int] )

-isnot

IS not type (e.g. $a -isnot [int] )

-as

convert to type (e.g. 1 -as [string] treats 1 as a string )

..

Range operator (e.g.  foreach ($i in 1..10) {$i }  )

&

call operator (e.g. $a = "Get-ChildItem" &$a executes Get-ChildItem)

. (dot followed by a space)

call operator (e.g. $a = "Get-ChildItem" . $a executes Get-ChildItem in the current scope)

-F

Format operator (e.g. foreach ($p in Get-Process) { "{0,-15} has {1,6} handles" –F  $p.processname,$p.Handlecount } )

Posted: 14 August 2006 14:12 by Paul Flaherty
Filed under: ,

Comments

DBMwS said:

For more information, if you have PowerShell installed, you can check out PowerShell help files;

For the usages of "IF" statements;
get-help about_if

For generic Operator help;
get-help about_operator

For Comparison-specific operators;
get-help about_Comparison_operators
# August 15, 2006 17:47

subject: exchange said:

I'm pretty sure I'm on vacations, that explains some lack of activity here lately.

The Inheritance...
# August 19, 2006 14:10

Flaphead on TechNet said:

Originally Posted here: http://blogs.flaphead.dns2go.com/archive/2006/08/14/3509.aspx Okay as a beginner...
# September 12, 2006 10:16

Percocet. said:

Identify percocet 93-490. Will percocet show up in a drug test. Generic percocet. Percocet.

# June 1, 2009 05:01

Adderall xr snort. said:

Adderall without a prescription. Adderall.

# June 3, 2009 02:49

Prescriptions drugs pharmacy online vicodin. said:

Forum vicodin buy vicodin online. Vicodin. Legal meds online usa vicodin. Buy vicodin online without. Vicodin no rx.

# June 7, 2009 03:34

Hydrocodone. said:

Hydrocodone. Buy hydrocodone without prescription. Alcohol hydrocodone. Ic hydrocodone apap 5 500. 3190 v hydrocodone.

# June 8, 2009 03:25
New Comments to this post are disabled