PowerShell Operators

by Andy Barber 7. December 2009 22:40
PowerShell 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
-not logical not equal
! logical not equal
-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)
. .Period or .full stop for an objects properties
$CompSys.TotalPhysicalMemory
-F Format operator (e.g. foreach ($p in Get-Process) { "{0,-15} has {1,6} handles" –F  $p.processname,$p.Handlecount } )


PowerShell Conditional or Comparison Operators


Operator Definition
-lt Less than
-le Less than or equal to
-gt Greater than
-ge Greater than or equal to
-eq Equal to
-ne Not Equal to
-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

Bitwise
-band Bitwise AND
-bor Bitwise OR
-is Is of Type
-isnot Is 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

 

Tags: , , ,

PowerShell

Running a PowerShell Script with a Space in the Path

by Andy Barber 4. August 2009 13:44

Taken from Dave Donaldson's blog at:  http://arcware.net/running-a-powershell-script-with-a-space-in-the-path/

Like with any new technology, getting started with PowerShell was not without a few hiccups. One of the first issues I ran into was how to properly pass script locations to PowerShell so that it could actually run them.

It turns out that PowerShell is a bit finicky in how it locates the script to run. You basically have two options for getting PowerShell to find and run your scripts:

  1. Explicitly pass the full path of the script to PowerShell, or
  2. Add your script location to the PATH environment variable so that you can just pass the script name

As simple as those sound, there's still a catch: if the path to your script contains a space, PowerShell won't be able to find it and will throw an error, unless you use some special notation. For example, below I'm trying to run a PowerShell script where the path has a space in it:

powershell1

Enclosing the path in quotes works with your basic cmd.exe, but not in PowerShell. To get this to work in PowerShell, you need to use notation that follows this format:

"& '[full path to script]'"

I told you the notation was special. So now I have this, which will run the script just fine:

powershell2

And there you have it. You can bet I've filed this one away permanently.

Tags: , ,

PowerShell

PowerShell Resources

by Andy Barber 13. May 2009 17:46

Tags: , , ,

Microsoft | PowerShell

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen