In: Computer Science
Task 6: Using whatif and confirm
Windows PowerShell allows administrators to safely test and use commands. In this step, you will use the whatif and confirm commands.
The [Command] -WhatIf flag shows you the results without actually performing the action.
The [Command] -Confirm flag asks you to confirm the operation before it executes.
1. Type the following command, and then press ENTER to see a list of services that would be stopped if you ran the Stop-Service command.
PS >Stop-Service M* -WhatIf
A list of services that would be stopped is displayed.
2. Type the following command, and then press ENTER to confirm whether or not to stop each service.
PS >Stop-Service M* -Confirm
For each service beginning with M, you will be asked if it should be stopped. For this lab, reply No in each case.
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.
PS >$var = 1,2,3
PS > $var This time, the variable is holding an array of integers.
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.
Executed all commands as asked. Please consider upvoting
1. Type the following command, and then press ENTER to see a list of services that would be stopped if you ran the Stop-Service command.
PS >Stop-Service M* -WhatIf
2. Type the following command, and then press ENTER to confirm whether or not to stop each service.
PS >Stop-Service M* -Confirm
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"
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.
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 = 1,2,3
PS > $var This time, the variable is
To see the new type of the variable, type the following command, and then press ENTER.
PS >$var.GetType().FullName
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"
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.