Powered By Blogger

Jul 26, 2012


PowerShell - Special Characters


This blog post is about special characters and tokens that we can use in PowerShell. I have listed  some of them and use of those characters and tokens with examples


  • $ : This Dollar mark use to declare a variable
      Eg: $number = "333"


  • $?: The Dollar mark and the question mark together use to indicating whether the previous command has an error or not by returning 'True' of 'False' as the output.
      Eg: See if a PowerShell cmdlet is existing in the system



  • | : The pipeline uses to get the output of the first command and pass it to another command to get the result.
      Eg: Get a list of processes and display the first 5 processes of the list



  • .. : Double dots use to specify a range. This can be use instead of foreach loop
      Eg: print numbers from 1 to 10 (1..10)



  • :: : The double colon use to refer a static member of a class
      Eg: Compare the two numbers "22" and "33" and gives 'False' as the output


  • % : The percentage mark acts as the MOD 
      Eg: 5%2 gives 1 as the output

  • ? : Question mark gives all the items which confirm the condition. This is same as 'where' 
     Eg: Print odd numbers between 1to 5




  • # : Single line comment
      Eg: #This is a single line comment


  • <# .. #> : This is for Multi- line comment
     Eg: <# this is a
           Multi-line
           comment#>


  • & : Ampersand use to execute a string as command
     Eg: & "Get-Process"






No comments:

Post a Comment