Question: Task 7: Creating and manipulating variables In this step, you will work with variables in Windows PowerShell. You will learn about their declaration, usage, and
Task 7: Creating and manipulating variables
In this step, you will work with variables in Windows PowerShell. You will learn about their declaration, usage, and behavior.
1. To create a variable to hold a string value, type the following command, and then press ENTER.
PS >$var = "Hi there"
With this command, you created a variable named var, and you assigned the string value Hi there to it.
A variable in Windows PowerShell must begin with the dollar sign ($). If special characters are needed in a variable name, curly braces can be used to surround the variable name ({}).
2. To output the value stored in this variable, type the following command, and then press ENTER.
PS >$var
A better way to output variable values is to use a cmdlet named Write-Host before the variable name, clearly showing it will output the values to the host.
PS >Write-host $var
By default, a variable will have a null value. Null values in Windows PowerShell are represented as $null.
3. Variables in Windows PowerShell can be listed and accessed under a special location. To display the list of currently declared variables, type the following command, and then press ENTER.
PS >Get-Variable
4. By default, variables in Windows PowerShell are non-typed, which means they can hold an arbitrary value. To change the variable value and type to an integer, type the following command, pressing ENTER after each line.
PS >$var = 123
PS > $var
Now the variable is holding the integer value 123.
5. Variables can also contain lists (similar to arrays). To change the value to an array of integers, type the following commands, pressing ENTER after each line.
PS >$var = 1,2,3
PS > $var
This time, the variable is holding an array of integers.
6. To see the new type of the variable, type the following command, and then press ENTER.
PS >$var.GetType().FullName
The variable is now an array object of type System.Object[].
Arrays can be also manipulated through their .NET methods. For example, they can be queried on their length.
7. To use the Length property to retrieve the number of elements of the array, type the following command, and then press ENTER.
PS >$var.Length
The size of the array is displayed.
8. You can also access individual elements within an array by using square brackets ([]). To retrieve the second element of the array, type the following command, and then press ENTER.
PS >$var[1]
Arrays in Windows PowerShell are zero-based, which means that the first element will always be at position (index value) 0.
The value of the second element of the array is displayed.
9. You can also type a variable by prefixing its declaration with the desired data type name. To re-declare the variable as an array of integers, type the following command, and then press ENTER.
PS >[int[]] $var = (1,2,3)
10. To assign the string value 0123 to the third element of the array, and then display it, type the following commands, pressing ENTER after each one.
PS >$var[2] = "0123"
PS > $var[2]
11. When an implicit conversion is not available, it displays an error. To attempt to set a string value that cannot be converted, type the following command, and then press ENTER.
PS >$var[2] = "A string value"
The error is displayed.
USE WINDOWS POWERSHELL, SHOW INPUT AND OUTPUT AND A SCREEN SHOT FOR EVERY STEP MADE
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
