In this lesson, we're going to take a look at
how you can do coding inside of PowerShell.
First, let's talk about commenting your code.
Just like we did in Bash, we can use the hashtag
to comment a line of code.
So if I use hashtag, this is the first line of my script,
that piece of code, everything beyond that hashtag
on that line is going to be ignored.
Now, in addition to having a single hashtag,
you can also use a less than sign
and a hashtag as an opening tag.
Then everything you write after that point
will be ignored until you end or close that tag
by doing #>.
So for example, if I wanted to have a comment block
that said, "This is a comment block, you can use this
to comment out large sections of text
or code in your scripts."
I would simply put <# all that text
and then #> to end that block of code.
The next thing we're going to talk about is variables.
In PowerShell, all variables are going to start
with a dollar sign.
So if you want to assign a variable,
you'll simply do dollar sign, the variable name,
equals the value.
For example, $CustomerName = Jason.
Now, if you want to display the contents of that variable
or interact with that variable using other commands,
you're simply going to call it using
the dollar sign and its name.
So in this case, it's $CustomerName to be able to read data
from that variable or display it to the screen.
You can also declare variable types
inside a PowerShell too if you want.
For example, if you want a variable to only hold integers,
you can define it by putting [int]$
and the variable name
like AnswerNumber=42 or whatever the value is you want.
In this case, I want to make sure that this AnswerNumber
will only take a number like 42 or 52 or one as its input.
If I want to do the same thing with a string,
I'll simply use
[string]$AnswerString = quote
and the string I want to save.
For example, "The life, the universe and everything."
If you want to declare a constant in PowerShell,
you do this by simply making a variable read only.
To do this, you're going to use the commandlet Set-Variable,
the name of the constant, in this case, Pi -Option,
and the option you want, in our case, ReadOnly.
Then -Value and the value you want to set.
In my case, 3.14159.
This gives me a single complete line of code that says
Set-Variable Pi -Option ReadOnly -Value 3.14159
to set the constant of Pi to the value of 3.14159.
PowerShell also supports the use of basic or indexed arrays.
This will allow you to store multiple values
and then reference them from a single name
and essentially make a list of variables.
For example, if you use
$tempArray = @(),
this will create a blank array called tempArray
that is ready to start storing data.
If you want to go ahead and create that array
and put data in it at the same time,
you can do that as well by using
$tempArray = @ ('Jason',
'Sahra', 'Eduardo', 'Linda', end parenthesis.
And this will store the four names of Jason,
Sahra, Eduardo and Linda into an array called tempArray.
If I want to get data back out of that array, I can read it
by using $tempArray, bracket and the position number.
For example, I'm going to use $tempArray[1]
and that would return the value of Sahra.
You could also use named arrays
or associate arrays inside of PowerShell.
For example, let's create one called PhoneBook
to store names and numbers.
To do this, we're going to use $PhoneBook = @
and then a set of curly brackets.
If I want to save the first name as Jason, I can do this
by doing $PhoneBook.name = 'Jason'.
If I want to store the number for that,
I can then use $PhoneBook.number = '321-123'.
If I want to be able to read the information
of Jason back out, I can do that
by doing $PhoneBook.name and essentially referencing
that key of name to the word Jason.
If I wanted to save all of this at once
using one command when I initialize it, I can do that too
by doing $PhoneBook = @{'name'='Jason',
'number'='321-1234'}.
Now, if I wanted to get the number out of that,
I can simply type in $PhoneBook.number
and the number of 321-123 would come back out.
Next, we need to talk about comparisons in PowerShell.
Just like in Bash, we are going to to use textual letters
to define what we are comparing
and in fact, they're the exact same ones as Bash.
This would be things like -eq for equal, -ne for not equal,
gt for greater than, -ge for greater than or equal to,
lt for less than or -le for less than or equal to.
If you want to use these statements,
you simply are going to put the variable, dash,
the thing you want to use, and then the variable name.
For example, to compare variable A against variable B
to see if they're equal,
I would simply type in $a -eq $b
and that will compare A against B.
If they are equal, I'll get a value back of true.
If they are not equal, I will get a value back of false.
Next, we have to talk about
our conditional statements like ifs.
Now, when we deal with an if inside a PowerShell,
it's going to look just a little bit different
than we used in Bash.
When we're using a conditional statement of if,
we're not actually going to have to type out the word then.
To create a conditional statement inside a PowerShell,
we're going to do if, parenthesis, the condition we want to test,
end parenthesis and then a curly bracket
to open up the then part of it.
Anything in between this set of curly brackets
is going to be considered the then commands
that we're going to operate.
Now, if you want to be able to use an else condition,
you would use if, condition, curly bracket,
the commands you want to run, end curly bracket, else,
curly bracket, the commands you want to run,
end curly bracket.
If you want to do multiple cases,
you can do things like if, elseif, else,
and in this case, it'll be if, the condition,
the curly bracket with the code, elseif, the condition,
the curly brackets with the code, else, the curly brackets
and the code you want to run.
As you can see, they're not very complicated,
but they are just a little bit different
than what you learned in Bash.
Next, let's talk about flow control,
like for, do while and while loops.
First, we're going to talk about a for loop.
Now, inside a PowerShell we're going to use a for loop
to perform a set of commands for each item in a list.
For example, if I use the format of for, parentheses
and then the initialization value, semi colon,
the condition I want to test, semi colon
and how often I want to repeat,
that would be my setup for the for.
Then I'm going to use a curly bracket and the statement
or commands I want to run and then end that curly bracket.
For example, if I wanted to print some value
to the screen multiple times as I go through the for cycle,
I could do something like for parenthesis $i=1
to initialize the i variable with a value of one.
; $i -lt 5; which test the condition
is the current value of i less than five.
Then we have $i++ and end the parenthesis.
This will then at the end of each loop increment the value
of i by one.
Then we have a set of curly braces and inside of that,
we're going to have the command we want to run.
In this case, Write-Host $i,
which will display the value of i
to the console on the screen.
Then when we're done with that for loop,
we will Write-Host "All done" to the screen.
So if I ran this code,
what you're going to see on your screen is 1234All done.
Now, why did it stop at four?
Well, the first time through the loop, i equal one,
We test as i less than five. Yes, it is.
We run through it and then increment by one.
Now, we're at two. We do the same thing for two.
Two is less than five., we print it to the screen.
We did an increment. We then go to three.
Three is less than five.
We print it to the screen and increment it.
Now, we're at four, four is less than five.
We increment it.
Now, we make it five. Is five less than five?
No, it's not, so we're not going to print five to the screen
and instead, we'll print All done.
The next loop we have is what's called a do while loop.
A do while loop is going to be used to perform
a set of commands while a test is true.
So we're always going to run things at least once
and then we're going to test the condition.
The way this format works is do, curly bracket,
the commands you want to run, end curly bracket, while,
parentheses, the condition you want to test.
So if I set the variable I to equal one
and then enter a do while loop,
I'm going to run the command Write-Host $i
and print i to the screen.
Then I'm going to increment i by using $i++
and then I'm going to check that condition.
Now, is i less than or equal to 10?
Yes, it is, because the value is only two at this point.
We've run through it once and we incremented our counter.
Now, we're going to go and print it again.
Two gets printed to the screen, we increment by one,
we're now at three.
Is three less than 10? Yes, it is.
So we'll go through the loop again.
So we print three to the screen, we increment to four,
we test that and we continue on until we get to nine.
Once i equals to nine, we test that condition.
It is still less than 10, so we'll print nine to the screen
and then increment to 10.
Now, is 10 less than 10? No, it's not.
So we're going to stop and print All done.
So what you would see on the screen is
123456789All done.
The third type we have is the until do.
With until do, we're going to perform a set of commands
until a test is considered true.
So this takes the format of do, curly bracket,
the commands you want to run,
end curly bracket, until, parenthesis,
the condition you want to test, end parenthesis.
So again, if I use $i = 1 and I set up a do,
{Write-Host $i $i++} Until ($i -gt 5)
Write-Host "All done".
What would you expect to see on the screen?
Well, you should expect it to have 12345All done.
Now, why is that?
Because we're going to continue to do this loop
of printing something and incrementing by one
until i is actually larger than five.
Once that statement becomes true,
that's where we're going to stop
and that statement won't become true
until i is equal to six, which happens right after
five is printed to the screen.
Next, we need to talk about string operations.
Now, with string operations, you're going to be able
to set values of a variable to a string
by putting that content inside of quote marks.
For example, $testString = "Test String".
Now, if you want to display this to the screen,
you would simply use Write-Host $testString.
But you can also concat or add things to strings.
For example, if I wanted to make this say
Test String2, I can do that by typing in
Write-Host $testString + "2"
and that would display Test String2 on the screen.
Also, when you're working with a string,
you can also pull out just certain pieces of a string
based on their position and the length you want.
To do this, you'll use the substring command.
When you want to display a variable with a substring,
you'll put $testString(2,4) as an example.
This is going to start at number two position
of the test string and pull out the next four characters.
So if I ran this against my test string,
what would I expect to get on the screen?
I would expect to get st S,
because I'm going to start at the second position,
which if you remember computers start counting at zero.
We're going to go zero, one, two.
That gets me to TES, so it's going to start at S.
Then I'm going to go for four places, st space S,
and that gives me the four places
I want to display to the screen.
Next, let's talk about input and output
from the keyboard and monitor.
If you want to put something out onto the screen,
you're going to use Write-Host
and then put that thing on the screen.
If you want to read, you're going to use Read-Host.
So putting this together, I might have something
that looks like this, Write-Host "Please enter your name:"
Then Read-Host $UserName.
This is going to print something to the screen
and then ask for the username from the person to enter it.
When they do that, I'm then going to use
Write-Host "Hello " + $UserName + "!"
So in total, on the screen you should see,
please enter your name, you'll type in your name
such as Jason and hit enter.
Once you do that, it's going to combine
or concatenate these three strings.
Hello, the username you entered
and the exclamation mark to print Hello Jason!
onto the screen.
Now, in addition to reading from the keyboard
and writing to the monitor,
you can also read and write files.
To do this, you can read a file by setting it
into a variable such as $TempFile = Get-Content - Path
and the file name you want such as C:\test.txt.
If I want to display the contents of that file
to the screen,
I can use something like Write-Host $TempFile
and that will show up on my screen.
In addition to this, we can also redirect information
into files by using the greater than sign
or the double greater than sign.
If you use a single greater than sign,
it's going to overwrite your file.
So for example, if I use
Write-Host "This is the beginning of a new script log file"
and then > script.log.
It's going to overwrite any content inside of script.log
and only write the text that I just listed
inside of this command.
Now, if I wanted to append something
to the end of this script, I can do that
by using the double greater than sign.
For example, let's say I wanted to run a script
called enumerate.ps1.
I would then say .\enumerate.ps1,
and then I can append its results into the log file
I just created.
So I would use .\enumerate.ps1 >> script.log.
As you can see, a lot of PowerShell looks very similar
to Bash with just a few slight differences.
One of the big giveaways that you're going to see
when you're dealing with PowerShell
is inside the script, you're going to see commandlets listed
such as Write-Host, which have a verb noun format.
For example, Read-Host, Write-Host.
This is an action followed by the thing you're doing
the action on.
Most commandlets inside a PowerShell
are going to be written this way,
and so that tends to be the giveaway
when you're looking at a script to determine
if it's a PowerShell script or not.