Tag Archives: knockout tutorial

PowerShell Comments

Same like other programming language, the comments in Powershell will not executed. Comments can be added to explain the function of the code and it can  be placed anywhere and anything between them will be treated as a comment. The powershell also supports single line and multi line comments.

Single Line Comments

SYNTAX: #

The comment with the # will handling single line comments. In PowerShell single line comments start with a hash symbol and everything to the right of the # will be ignored.

$MyFirstVaiable = "Hello" #Example for single line comments.

Multi-line Comments

Begin the comment with the <# tag, and end the comment with the #> tag for handling multiline. Multi-line comments are typically used to add descriptive help at the start of a script.

SYNTAX: <# #>

$MyFirstVaiable = "Hello" <# Example for Multiple line comments. 
                  This variable use to display hello #>

 

Working with PowerShell’s Data Types

An important property of a PowerShell variable is its name, which is always preceded by the dollar sign “$” and can only contain letters, numbers, and the underscore. If you feel a strong urge to use other characters, you have to enclose the name in curly braces. You should not use the name of variables that have been pre-defined.

Windows PowerShell uses the Microsoft .NET Framework data types. The poweshell can supports strings ,integers ,floating point numbers, strings, and Boolean values.You don’t have to explicitly declare the data type of a variable, the PowerShell automatically chooses the data type for you when you initialize the variable—that is,when you first assign a value.

Example: 1 

$GetNumber = Read-Host "Please enter you score" 
$Total = $GetNumber + $GetNumber
Write-Host "The Total Score is $Total"

PowerShell wrongly assumed that the data type of the variable $GetNumber is String. Because the arithmetic operator + is overloaded (the implementation of the operator depends on the arguments), the second line in the program add a string instead of a number.

The second argument of the operator + always has to be a number, so PowerShell automatically converts the data type of $GetNumber into Int32. However, the first argument can also be a string. The result is that PowerShell determines the value of the expression “5”+5, which is 55 ( concatenate the string). 

Example: 2

[INT]$GetNumber = Read-Host "Please enter you score" 
$Total = $GetNumber + $GetNumber
Write-Host "The Total Score is $Total"

In the above code snippet, we explicitly declared the number as Int32 (integer) by enclosing the type name in square brackets before the variable name. So the above variable is called “strongly typed”. If you declare the variable’s data type implicitly in our script the the variable is called as weakly typed.

Data Types:

Data Type Name Description
[Array] Array
[Bool] Value is TRUE or FALSE
[DateTime] Date and time
[Guid] Globally unique 32-byte identifier
[HashTable] Hash table, collection of key-value pairs
[Int32], [Int] 32-bit integers
[PsObject] PowerShell object
[Regex] Regular  expression
[ScriptBlock] PowerShell script block
[Single], [Float] Floating point number
[String] String
[Switch] PowerShell switch parameter
[TimeSpan] Time interval
[XmlDocument] XML document

Note: 

  • Windows PowerShell uses the Microsoft .NET Framework data types.
  • We don’t have to rely on PowerShell’s ability to automatically convert data types if we tell the interpreter that we are expecting a number as input.
  • This ensures that our script works as intended.
  • The explicitly declaring variable types can prevent unwanted results in your scripts and makes them more reliable.

PowerShell’s Variables

Windows PowerShell is designed to be an interactive command-line shell and it’s also a programming language. A PowerShell script is really nothing more than a simple text file. The file contains a series of PowerShell commands, with each command appearing on a separate line.

If we need to run the PowerShell script using the notepad then its filename needs to save as .PS1 extension.

Powershell Variables

Is It Case Sensitive

The answer is “YES”, power shell comments are case sensitive. 

  • As like other programming language, we need to declare the variable as like below. The main difference is we need to add prefix with ‘$’ symbol in the variable.
  • All we have to do to declare those variables is add a dollar sign, then use whatever name for the variable we want and no spaces are allowed in the variable name.

Example : 1

$name = 'Jon'
$number = 12345
$myGrade= 'D+'
$location = 'Charlotte'
// if we declare as string then it will treat as single word
$listofnumbers = 6,7,8,9

Example : 2

//Assign text to varible
$myFirstVarible = "Dotnet-helpers Welcomes you"
//Print the variable
$myFirstVarible 
//Concatenate Variable
$mySecondVarible = $myFirstVarible + "for PowerShell learning Curve" 
$mySecondVarible

Example : 3 Handling Dynamic Variables.

The Get-Variable cmdlet gets the PowerShell variables in the current console. You can retrieve just the values of the variables by specifying the ValueOnly parameter.

$UserAnna= “This is specific MSG for Anna”
$inputUser=”Anna"
$msgAnna = $(("User"+ $inputUser))
#-ValueOnly : Indicates that this cmdlet gets only the value of the variable.
Get-Variable -Name "$msgAnna" -ValueOnly

OUTPUT: This is specific MSG for Anna

 

Introduction to Window’s PowerShell

Windows PowerShell is one of the most important Windows Server management tools that Microsoft has released. Windows PowerShell is one of the most important Windows Server management tools that Microsoft has released. An entirely new scripting environment for Windows, it consists of a new command shell and a new scripting language.We can write PowerShell scripts using any text editor, but you must need the PowerShell command shell for running.

Why its called Window Server Management?

Although PowerShell has been important in the Microsoft ecosystem ever since its release, as Windows Server 10 comes closer to release,we find that many features and deployments are significantly easier and more full-featured when carried out with PowerShell.  

How to get License?

PowerShell is built into Windows, so there is no fee or additional licensing cost. 

More about PowerShell:

Windows PowerShell is a command-line shell and scripting language designed specially for system administration. Built on the .NET Framework, Windows PowerShell helps IT professionals to control and automate the administration of the Windows operating system and applications that run on Windows Server environment. In PowerShell, administrative tasks are generally performed by cmdlets, specialized .NET classes implementing a particular operation. Sets of cmdlets may be combined together in scripts; executables, which are standalone applications; or by instantiating regular .NET classes.

Power shell versions: 

PowerShell Version Release Date Default Windows Versions
2.0 October 2009 Windows 7 Windows Server 2008 R2
3.0 September 2012 Windows 8 Windows Server 2012
4.0 October 2013 Windows 8.1 Windows Server 2012 R2
5.0 April 2014 Windows 10