Thursday, 26 November 2015

Why ' $_ ' Represents an Object ?


One of the key factor of PowerShell which made it most popular and successful Scripting language for management id the ability to process and handle everything as Objects and passing them throughout the pipeline .

Yes, The Pipeline. Lets check with an example.

PS C:\>Get-Service | Where-Object -FilterScript {$_.Status -eq 'Running'}

Status   Name               DisplayName
------   ----               -----------
Running  ALG                Application Layer Gateway Service
Running  AppHostSvc         Application Host Helper Service
Running  Appinfo            Application Information
Running  AudioEndpointBu... Windows Audio Endpoint Builder
Running  Audiosrv           Windows Audio
Running  BFE                Base Filtering Engine
Running  BITS               Background Intelligent Transfer Ser...
Running  BrokerInfrastru... Background Tasks Infrastructure Ser...
Running  Browser            Computer Browser
Running  CertPropSvc        Certificate Propagation
Running  ClipSVC            Client License Service (ClipSVC)
Running  CoreMessagingRe... CoreMessaging
Running  CryptSvc           Cryptographic Services
Running  CscService         Offline Files
Running  CxAudMsg           Conexant Audio Message Service
Running  DcomLaunch         DCOM Server Process Launcher
Running  DeviceAssociati... Device Association Service
Running  Dhcp               DHCP Client
Running  DiagTrack          Diagnostics Tracking Service
Running  Dnscache           DNS Client
Running  DPS                Diagnostic Policy Service
Running  DsmSvc             Device Setup Manager
Running  DsSvc              Data Sharing Service
Running  EFS                Encrypting File System (EFS)
Running  EventLog           Windows Event Log
Running  EventSystem        COM+ Event System


Here,we Have the services in My Computer which are currently running.

We used  Where-Object cmdlet to select only Running Services and -FilteScript Parameter to filter the output of Get-Service cmdlet from the pipeline. The criteria given is {$_.Status -eq 'Running'} .

Lets have a look, why " $_.Status " .

PowerShell cmdlets returns output in the form of table(based on Formatting conditions) . We can See Table headrers , Rows , Colums etc .



Here $ is the output from Get-Service cmdlet , $_ represents each row in that output and $_.Status represent a row at a time but only the value for Status . Here " . " represents a fraction of that row.



Finally we got Only running services, PowerShell Parser takes each Row in that Table and will select status for each service then do an IncaseSensitive comparision using operator -eq to mach the string Running.



Friday, 16 October 2015

Strings in PowerShell


Like all programming languages, PowerShell uses strings very effectively and provides some interesting capabilities for a script.

PowerShell uses four types of strings.
  1. Single quoted string.
  2. Double quoted string.
  3. Single quoted Here Strings.
  4. Double quoted Here Strings.
PowerShell uses strings as sequence of 16-bit Unicode characters and is directly implemented by system.string .Net type .

Single Quoted Strings
     Single quoted string in PowerShell are nothing, But string which is represented inside single quotes.


    PS C:\ >'This is a Single Quoted string'
    This is a Single Quoted string
    PS C:\ >

    But there is some more about single quoted string,
    lets see this example,

    PS C:\ >'This is a Single quoted $var'
    This is a Single quoted $var
    PS C:\ >
     
    
    Here you can see, I have stored 'String' in a variable named $var , But single quoted string is not able to substitute the value of the $var .
    So we can use single quoted strings where we don't want to substitute a variables value in it.


    Double Quoted Strings.
      Like single quoted strings, Double quoted string are nothing , but Strings included between two double quotes .
      PS C:\ >"This is a Double Quoted string"
      This is a Double Quoted string
      PS C:\ >
      
       
      
      But unlike single quoted strings, Double quoted strings can substitute Values for Variables.
      PS C:\ >"This is a Double quoted $var"
      This is a Double quoted String
      PS C:\ >
      

      Here Strings

      First Lets have a glance on Here Strings.
      Here strings are another way of representing strings , Especially while we have large amount including multiple lines. Here strings are effectively used when generating output for another program. But the same can be achieved regular type of strings which we discussed before.
      Then Why Here Strings ?

      Lets explain it after an example,
      PS C:\ >$HereString=@"
      >>> This is
      >>> An Example
      >>> For
      >>> Here String
      >>> "@
      PS C:\ >$HereString
      This is
      An Example
      For
      Here String
      PS C:\ >
      

      Here we Can see the syntax for here string.

      @"<newline><Strings><newline>"@

      the <newline> is important as strings between the quotes  are treated as Here Strings and the first and last new lines are not included and the main advantage is , we can include single and double quotes regardless the type of Here Strings whether it is single Quoted or Double Quoted.

      Look here on another example,

      PS C:\ >$a='This'
      PS C:\ >$HereString=@"
      >>> $a is
      >>> An "Example"
      >>> For
      >>> 'Here String'
      >>> "@
      PS C:\ >$HereString
      This is
      An "Example"
      For
      'Here String'
      PS C:\ >

      Here the variable is substituted for $a, Single and Double quotes are included too !!!.

      And Single Quoted Here Strings are here strings using single quotes where variables are not substituted with values - @'<newline><string><newline>'@.
      Double Quoted Here Strings are similar, But uses Double quotes instead of single quotes and the variables are substituted here.
      @"<newline><string><newline>"@

      That's all about Strings here... Have fun Using PowerShell