PowerShell Operators

PowerShell's Operators

Operator

Definition

## The hash key is for comments
+Add
-Subtract
*Multiply 
/Divide
%Modulus (Some call it Modulo) - Means remainder 17 % 5 = 2 Remainder
=equal
-notlogical not equal
!logical not equal
-bandbinary and
-borbinary or 
-bnotbinary not
-replaceReplace (e.g.  "abcde" –replace "b","B") (case insensitive)
-ireplaceCase-insensitive replace (e.g.  "abcde" –ireplace "B","3")
-creplaceCase-sensitive replace (e.g.  "abcde" –creplace "B","3")
-andAND (e.g. ($a -ge 5 -AND $a -le 15) )
-orOR  (e.g. ($a –eq "A" –OR $a –eq "B") )
-isIS type (e.g. $a -is [int] )
-isnotIS not type (e.g. $a -isnot [int] )
-asconvert 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)
..Period or .full stop for an objects properties
$CompSys.TotalPhysicalMemory
-FFormat operator (e.g. foreach ($p in Get-Process) { "{0,-15} has {1,6} handles" –F  $p.processname,$p.Handlecount } )

PowerShell's Conditional or Comparison Operators

Operator

Definition

-ltLess than
-leLess than or equal to
-gtGreater than
-geGreater than or equal to
-eqEqual to
-neNot Equal to
-containsDetermine elements in a group. 
This always returns Boolean $True or $False.
-notcontainsDetermine excluded elements in a group
This always returns Boolean $True or $False.
-likeLike - uses wildcards for pattern matching
-notlikeNot Like - uses wildcards for pattern matching
-matchMatch - uses regular expressions for pattern matching
-notmatchNot Match - uses regular expressions for pattern matching
 Bitwise
-bandBitwise AND
-borBitwise OR
-isIs of Type
-isnotIs not of Type
 Other Operators
if(condition)If condition
elseIf(condition)ElseIF
else(condition)Else
>Redirect, for example, output to text file
Example   .\cmdlet > stuff.txt
>>Same as Redirect except it appends to an existing file

posted @ 2010-02-06 15:18  Vincent Yang  阅读(712)  评论(0编辑  收藏  举报