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
1 2 3 4 5 6 |
$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
1 2 3 4 5 6 7 |
//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.
1 2 3 4 5 |
$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
Leave A Comment