Tuesday 22 September 2015

PowerShell Basics

Powershell is an object-oriented programming language and interactive command line shell for Microsoft Windows Platforms.
We have an environment(Windows PowerShell ISE editor) as below to write code in PowerShell .


One of the greatest feature is its integration with Microsoft.Net environment and it can also integrate with other applications .
  • Cmdlets are very small .NET classes that appear as system commands.
  • Scripts are combinations of cmdlets and associated logic.
Simple Powershell script to Write a Hello message:
 write-host "Hello"
Note: It is not mandatory to define the type of variable, declare a variable before use and it is also case insensitive as we have in VBScript. Dont compare PowerShell with VBscript as we can execute a .vbs file with just double clicking it.PowerShell script can be either run from PowerShell command prompt or using a batch file.

Variable Declaration: Variable in PowerShell script begin with a $ sign.
Ex: $Testval = 10;
      write-host ("Value assigned is: " + $Testval );


Array Declaration:
Ex: $Testval = 1,2,3,7,8;
      foreach($val in $Testval )

      {
             write-host (" Value assigned is: "+ $val );
     }
 or

      for($val =0; $val - lt  $Testval.Length;$val++ )
      {
          #Write the output values to command window.
           write-host  $Testval[$val]
     }
 

Associative Array: This is similar to hash table data type where the data is maintained in key value pairs.
 Syntax: $<array name> = @{<keyname = value>;.......}
             Ex:$Testval = @{'0'= 'Sunday' ; '1' = 'Monday'; '2' = 'Tuesday'; '3' = 'Wednesday' }
                   $Testval  //to print all key value pairs at one go.
                  $Testval ['0'] //prints Sunday
            Add a new record : $Testval ['4'] = 'Thursday'
            Delete a record from collection : $Testval.Remove('1')
            To remove all records :  $Testval.Clear()
While Statement :
       $val=1
        while ($val -ne 3)
        {
              $val++
       }

If Statement :
    if($val -eq 3)
        {
              write-host "Value is 3"
       }
     elseif($val -eq 4)
     {
              write-host "Value is 4" 
     }
   else
    {
             write-host "value is neither 3 nor 4"
    }

Logical Operators :

-and
logical and
(A -eq A) -and (A -eq B)
false
-or
logical or
(A -eq B) -or (A -eq A)
true
-not
logical not
(1 -eq 1) -and -not (2 -gt 2)
true
!
logical not
(1 -eq 1) -and !(2 -gt 2)
true


Comparision Operators :

-eq
Equal (case insensitive)
-ne
Not equal (case insensitive)
-ge
Greater than or equal (case insensitive)
-gt
Greater than (case insensitive)
-lt
Less than (case insensitive)
-le
Less than or equal (case insensitive)
-like
Wildcard comparison (case insensitive)
-notlike
Wildcard comparison (case insensitive)
-match
Regular expression comparison (case insensitive)
-notmatch
Regular expression comparison (case insensitive)
-replace
Replace operator (case insensitive)
-contains
Containment operator (case insensitive)
-notcontains
Containment operator (case insensitive)

-ieq
Case insensitive equal
-ine
Case insensitive not equal
-ige
Case insensitive greater than or equal
-igt
Case insensitive greater than
-ile
Case insensitive less than or equal
-ilt
Case insensitive less than
-ilike
Case insensitive equal
-inotlike
Case insensitive equal
-imatch
Case insensitive regular expression comparison
-inotmatch
Case insensitive regular expression comparison
-ireplace
Case insensitive replace operator
-icontains
Case insensitive containment operator
-inotcontains
Case insensitive containment operator

-ceq
Equal (case sensitive)
-cne
Not equal (case sensitive)
-cge
Greater than or equal (case sensitive)
-cgt
Greater than (case sensitive)
-clt
Less than (case sensitive)
-cle
Less than or equal (case sensitive)
-clike
Wildcard comparison (case sensitive)
-cnotlike
Wildcard comparison (case sensitive)
-cmatch
Regular expression comparison (case sensitive)
-cnotmatch
Regular expression comparison (case sensitive)
-creplace
Replace operator (case sensitive)
-ccontains
Containment operator (case sensitive)
-cnotcontains
Containment operator (case sensitive)

-is
Is of a type
-isnot
Is not of a type
-as
Is a type, no error if conversion fails


Some important functions or commands:
       get-date # returns the current date and time
       (get-date).day #returns day of the day, similary we can display month year etc.
       $StringVal -replace("a","b") #replace the charected "a" with "b" in the string $StringVal

No comments:

Post a Comment