Powershell Day 3

Day 3 Programming in Powershell Part 1

Now you got some good basic information about Powershell, and some basic commands cmdlets. In this blog we will discuss how to write some basic scripting.

  1. Arithmetic expression: all the arithmetic expression supports Powershell ie. +, -, *, / and %

Eg. Ps c:\>2+3

5

Ps c:\> 8%3

2

  1. Assignment operators: – Assignment operators are =,  +=,  -=, *=, /=, %=

PS C:\> $a=1

PS C:\> $b=2

PS C:\> $c=3

PS C:\> $d=4

PS C:\> $e=$a+$b

PS C:\> echo $e

3

PS C:\> $e+=$a

PS C:\> $e

4

PS C:\> $e -=$a

PS C:\> $e

3

PS C:\> $e *=$a

PS C:\> $e

3

PS C:\> $e /=$a

PS C:\> $e

3

PS C:\> $e %=$a

PS C:\> $e

0

  1. I/O Redirection:- we can redirect the i/o to or from other file .txt, also append:   (>, >>, 2>, 2>&1) it does not support input redirection ( get-Content)

PS C:\> dir > a.txt

  1. Comprison operator:
Operator Description Example Result
-eq –ceq –ieq Equals 5 –eq 5 $true
-ne –cne –ine Not equals 5 –ne 5 $false
-gt –cgt –igt Greater than 5 –gt 3 $true
-ge –cge –ige Greater than or equal 5 –ge 3 $true
-lt –clt –ilt Less than 5 –lt 3 $false
-le –cle –ile Less than or equals 5 –le 3 $false
-contains -ccontains

-icontains

The collection on the left hand side contains the value specified on the right hand side. 1,2,3 –contains 2 $true
-notcontains-cnotcontains

-inotcontains

The collection on the left hand side does not contain the value on the right hand side. 1,2,3 –notcontains 2 $false
         

 

  1. Format:

 

Cmdlet Description Example
Format-Table Formats a set of properties into a table dir | format-table name, length
Format-List Displays properties 1 per line in a list. dir | format-list *
Format-Wide Displays a single property in multiple columns dir | format-wide
Format-Custom Complex formatter dir | format-custom

 

  1. Output:

 

Cmdlet Description Example
Out-File Writes formatted text to a file dir | out-file –encoding unicode foo.txt
Out-Host Writes formatted text to the screen dir | out-host -pag
Out-Null Discards all output (equivalent to > $null) dir | out-null
Out-Printer Sends formatted output to the printer. cat report.ps | out-printer
Out-String Formats input as strings and writes them to the output pipe dir | out-string | where {$_. match “x”}

 

  1. Pipeline:- The pipeline is used to compose the output of the command.

PS C:\> dir | select -first 5

PS C:\> dir | select -last 5

PS C:\> dir | sort name | select -last 5

PS C:\> dir -filter *.bat

#display all sub directory as well

PS C:\> dir -filter *.bat –recurse

#to write comments

Advertisements
This entry was posted in Powershell and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s