>> Hello, everyone.
I'm instructor Gerri Roberts,
and this is PowerShell scripting.
In this video, we're going to
learn a bit about scripting.
First, we're going to learn what a script actually is,
how to script in PowerShell ISE,
how to use Notepad to script,
and some other scripting tools that are available.
First of all, what is a script?
Script is pretty much just a file with a command or
commands that you can run
instead of typing them out manually.
Pretty much what you're doing as you're copying and
pasting or typing out your commands into a file,
saving it so that way you can run
it later instead of having to
>> type everything out again.
>> Scripts can be run manually
or they can be set up to run on a schedule.
Scripts are typically used to
automate day to day processes like backups,
log on, log off scripts,
these are scripts that'll do things like
change your background and
change your settings when you log in,
also to be able to gather information about
computers and more administrative tasks.
PowerShell ISE for scripting.
PowerShell ISE is actually
meant to be used for scripting.
ISE actually stands for
>> Integrated Scripting Environment.
>> This is what PowerShell ISE looks like.
I have actually already modified
some things to add the command
window in the script window.
If you go at the top,
you can do View and you can actually
get rid of the script pane, put it back.
You can change where the script pane is at.
I could say, show a script pane over the right,
and there it is instead of at the top.
I personally like the script pane at the top.
I feel like it's a little bit easier to read.
I also like having the commands
added over here on the right so I can actually
look for my commands and then
your regular PowerShell window
is actually at the bottom here.
When working in PowerShell ISE,
on the script pane,
which is this pane right here,
you'll notice that it says untitled.ps1.
I haven't really done anything with it yet,
so there's no name for it.
PS1 is actually the PowerShell scripting file format.
If you write a file in a another program and need to
save it as a PowerShell script, remember that it's.ps1.
That's also if you're looking through
files and you find one that says PS1,
you know it's a PowerShell script.
Here I wrote a script
to get some information about the BIOS.
What you do in here is you
write your command or commands that you want,
say, for example,
I wanted to do more than that and get more information,
I could do Get and then Cim,
and you'll notice the nice thing
here is the IntelliSense tells us,
"Hey, guess what?
There is a command that matches.
There's actually a couple,
so you can go through here."
If you also notice,
when you put your mouse over it,
you'll be able to click on it or you
can just use the up and down arrows to choose one.
When you choose one, at first,
you'll notice you'll get that nice little information
like you do with the help command
in your regular PowerShell.
Once you have the one you
want selected, you hit "Enter",
and it will actually complete it
for you. You can see there.
[NOISE] Then whatever item you want,
if you wanted just the CPU information,
there'll be a processor.
Here, I'm going to format it as a list,
so it's easier to read.
I'm going to pipe a list
and then I'm going to pipe a file.
The first one has bio2.txt for the BIOS.
I could call this CPU2.txt,
so it pipes it to a file called CPU2.txt.
Remember, from our piping to file lesson,
if we pipe anything to a file,
the file doesn't have to exist.
If it does not exist yet,
what it will do is it
will actually create the file for you.
Once you have your script written,
there are a couple of ways to run.
Up here you'll notice that you
>> have a little play button.
>> Then you can also select part of
>> it and just play the part of it you want.
For example, I could just run
that by going there and clicking there.
If you want to run the whole thing,
you can do the run.
You can also go to File and Run,
or you can just do F5 to run it.
Once you run it,
you'll notice that down here on
your regular PowerShell that
you get a little bit of output.
You see that it ran that and it ran that.
You won't get much usually unless there's an error.
If there's an error, you'll get a big old wall
of red text.
Otherwise, it'll show that it ran it.
If I want to actually open up the file,
I can go to the directory or
down here in regular PowerShell,
I can just type in the name of the file since it's in
the same directory that we're working with.
I want to open this CPU1,
and notice that it opens for me.
Notice that this script created the file for me
and it gave me
the information for this CPU, which is pretty cool.
You can see this can be super useful if
you're working with computers
you need to get information about them.
That's pretty much PowerShell ISE.
Next, you could use Notepad for scripting.
Some people, they don't
necessarily want the whole interface,
they just want to write their script and that's it.
That's fine. You can do that in Notepad.
What you would actually do is
you would go ahead and open up Notepad.
Here you can see I created one already.
You can see I have a couple of pieces of
information here for a couple of items to run.
One thing you'll notice is that obviously in Notepad,
you're not going to get IntelliSense,
you're not going to get auto-complete,
you're not going to get all the fancy pretty things
you do in ISE,
but this is great if you just want to do
a quick and dirty PowerShell script and you're done.
What I have here is the file that I already created.
If I want to create a brand new one,
I could open up a new Notepad.
When I do this,
I can type in the commands I want.
I'm going to just get
some information about the computer.
Again, you notice that there's no IntelliSense.
Any typos I do or anything like that,
I have to go back and fix.
[NOISE] I'm going to get information about the desktop.
I'll format that list too again,
just because I want
to make sure that it's easier to read,
and then I can actually output it to a file too.
You'll see here I have a script that's going to
get information about the Win32 desktop.
I'll go ahead and save that.
When you save it, you'll
notice that it normally goes to text.
You'll want to change it to All Files
so you can save it as a proper,
and I'm going to save this as mytestscript2.ps1.
Remember, when you save it,
you have to put a.ps1 so it
knows it's a PowerShell file and save it.
There's nothing you can really do here in Notepad,
but if you were to go into your file directory here,
and you'll notice that it has.ps1.
If you were to right-click it,
you can choose "Run with PowerShell", and it'll run.
The PowerShell will pop up
>> and disappear pretty quickly.
>> You might not have even seen it,
but once it's run,
you'll notice we get this new file desktop2.txt,
which was the file, if you
remember looking at our PowerShell script,
that we are piping to.
Remember that we've got information about our desktop.
It's a little more complicated with
Notepad because you actually have to go find the file,
run it and all that good stuff,
but you can still do scripting in
Notepad if that's what you want to do.
We've looked at PowerShell ISE,
we looked at Notepad,
but there are a couple of other tools that you can use.
We have Notepad++,
which allows you to color-code
your commands so you can easily see what you're doing.
You also have Visual Studio that will
also automatically color-code your commands.
Visual Studio does have some IntelliSense items in it
like PowerShell ISE so it is super useful as well.
Post assessment time.
If you wanted to run a script you created in Notepad,
how would you go about doing it?
Would you find the saved PS1 file,
right-click "Run with PowerShell"?
Would you F5 in Notepad?
Would you go to File and Run in Notepad?
Or you can't run a script created in Notepad?
You can go ahead and pause,
and I'll give you a second to figure it out.
The answer is A.
You'd find the saved.ps1 file
and right-click, "Run with PowerShell".
B and C are used for PowerShell ISE,
and D is incorrect.
You totally can create a script in Notepad,
save it, and run it later.
That's it for this video.
We've talked about PowerShell
scripting and PowerShell ISE and Notepad.