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
      Here String
      PS C:\ >

      Here we Can see the syntax for here string.


      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"
      '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.

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

      Saturday 3 October 2015

      TAB completion in PowerShell

       PowerShell: TAB completion in PowerShell

      Reference : PowerShell in Action by Bruce Payette.

      One of the interesting feature in Windows PowerShell is the way of TAB completion. TAB completion in PowerShell allows you to partially enter a command and to hit TAB key to get it expanded cmdlet.

      PowerShell allows you to do TAB completion against file system, If you type part of a filename or path, a TAB completion can easily be done by hitting TAB key. And the interesting thing is doing TAB completion using wildcard '*' .
      C:\ >cd c:\win*ws< tab>
      C:\ >cd C:\Windows
      And PowerShell can do TAB completion on partial cmdlet names too. If you type a part of a cmdlet and then if pressed TAB, it will give you the first matching expanded cmdlet and continues TAB hit will step through to the possible matches.

      We have much more on TAB completion. PowerShell can do TAB completion on Parameters too. Lets see an example.

      C:\ >Get-ChildItem -pa <tab>
      C:\ >Get-ChildItem -Path
      Not only parameters, but on properties too. Have a look on this,
      C:\ >$r="A String"
      C:\ >$r.Len< tab>

      C:\ >$r.Length
      The string "A String" is stored in variable $r and then we check the length of the string using Length property. We use TAB completion here by pressing TAB after entering $r.Leng .

      We Know PowerShell has a
      Get-History cmdlet, Which lists the previously entered cmdlets.
      C:\ >Get-History

      Id CommandLine
         -- -----------
         23 Clear-History
         24 $r="A String"
         25 $r.Length
      Let's do something interesting,
      C:\ >#str< tab>
      C:\ >$r="A String"
      Here we typed #str and hit TAB key , Which returns a previously entered expression that contains 'str' and will step through next matching expression.

      We can get the same using the Id of the previously entered expression.
      C:\ >#24
      C:\ >$r="A String"

      To Explore more about Tab completion in PowerShell, Just go through the built-in  PowerShell function TabExpansion which is editable.