PowerShell
PowerShell
PowerShell is a scripting language that can be used on Windows Systems. You never know when you might gain access to a Windows system and might want to use a PowerShell script to enumerate information.
During this section we will cover:
Variables
Arrays
Comparison Operators
String Operations
Conditionals
Basic Loops
Error Handling
To continue to improve on your PowerShell skills Microsoft provides a great site.
As the Python Page provide details about what each element is we will just look at examples on how to use Ruby.
PowerShell scripts use a ps1 file extension.
Variables
PowerShell requires the use of a $ before a variable name. To create a variable and print the contents you would use the following syntax:
$a = Get-Process
Write-Output $a
Arrays
To create an array in PowerShell we use the @ symbol. It is a bit different in syntax then we are use to seeing. We will use a normal variable definition on the left side, but use the @ on the right side.
$fruit = @('Bananas', 'Mangos', 'Apples')
$fruit.count
$fruit[1]
The first line of code creates our array. The second line of code displays the number of elements in the array. The last line shows how we can access and print out values of our array. $fruit[1] would print out Mangos as arrays start at 0.
PowerShell Comparison Operators
PowerShell uses different comparison operators then we have seen in Ruby and Python. Be aware of this as it is an easy way to determine if it is a PowerShell script.
-eq (equal to)
-ne (not equal to)
-gt (greater than)
-lt (less than)
-le (less than or equal to)
-ge (greater than or equal to)
These operators are not case senstive by default. If you wish to make them case sensitive you need to prefix them with a c.
-ceq (equal to)
-cne (not equal to)
-cgt (greater than)
-clt (less than)
-cle (less than or equal to)
-cge (greater than or equal to)
String Operators
PowerShell has a lot of string operations. The best way to learn about it is to view the documentation provided by Microsoft. You can also learn what methods are available by doing the following:
"Hello World" | Get-Member
Hello World is our string and we are piping this data out to determine what operations we can perform on it.
A common String operation is concatenation. This combines two strings into one. See the example below:
$first_name = "Pete"
$last_name = "Gross"
$full_name = $first_name + " " + $last_name
Write-Output $full_name
Conditionals
Conditionals in PowerShell work the same way, except the syntax is a bit different due to the comparison operators.
if ( $condition )
{
Write-Output "The condition was true"
}
PowerShell also has an else statement that can be used:
if ( $condition )
{
Write-Output "The condition was true"
}
else
{
Write-Output "The condition was false"
}
We also cannot forget about the else if statement. The syntax for this differs, so be aware of it!
$discount = if ( $age -ge 62)
{
0.20
}
elseif ( $age -le 12)
{
0.15
}
else
{
0.00
}
This is a bit different syntax too as we are assigning a value to $discount based on the result of the if statement. This is a pretty cool feature that PowerShell supports! I probably wouldn't use this frequently, but did want to show this feature.
Basic Looping
PowerShell's for loop looks just like what you experienced in Java.
for ( $i= 0; $i -le 10; $i++ ) {
$i
}
While loops are the same as we have seen before too!
while (condition)
{
Code to be executed
}
Error Handling
PowerShell follows the same syntax as you experience in Java. PowerShell uses try-catch for handling errors.
try {
Code to be tested here.
}
catch {
"An error occurred."
}
You can also catch specific errors too. The syntax differs a little bit as you need to speificy the exception. For example, if you wanted to catch a WebException due to a file not able to be downloaded you would do the following:
try {
$wc = new-object System.Net.WebClient
$wc.DownloadFile("ics281.leewardics.rocks/myfile","c:\temp\myfile")
}
catch [System.Net.WebException],[System.IO.IOException] {
"Unable to download myfile from ics281.leewardics.rocks."
}
catch {
"An error occurred that could not be resolved."
}
PowerShell Wrap up
This is just a short introduction to PowerShell scripting. I would suggest practicing and learning more about PowerShell scripting. It is a powerful tool that you can add to your toolbox!