WEBVTT

00:00.000 --> 00:01.575
>> Hello, everyone.

00:01.575 --> 00:03.300
I'm instructor Gerri Roberts,

00:03.300 --> 00:05.205
and this is
PowerShell scripting.

00:05.205 --> 00:06.600
In this video, we're going to

00:06.600 --> 00:08.115
learn a bit about scripting.

00:08.115 --> 00:10.875
First, we're going to learn
what a script actually is,

00:10.875 --> 00:13.499
how to script in PowerShell ISE,

00:13.499 --> 00:15.870
how to use Notepad to script,

00:15.870 --> 00:19.390
and some other scripting
tools that are available.

00:20.060 --> 00:23.040
First of all, what is a script?

00:23.040 --> 00:25.950
Script is pretty much just
a file with a command or

00:25.950 --> 00:27.120
commands that you can run

00:27.120 --> 00:29.145
instead of typing
them out manually.

00:29.145 --> 00:31.580
Pretty much what you're
doing as you're copying and

00:31.580 --> 00:34.085
pasting or typing out your
commands into a file,

00:34.085 --> 00:36.260
saving it so that
way you can run

00:36.260 --> 00:37.370
it later instead of having to

00:37.370 --> 00:38.734
>> type everything out again.

00:38.734 --> 00:40.805
>> Scripts can be run manually

00:40.805 --> 00:43.510
or they can be set up
to run on a schedule.

00:43.510 --> 00:46.190
Scripts are typically used to

00:46.190 --> 00:48.994
automate day to day
processes like backups,

00:48.994 --> 00:50.810
log on, log off scripts,

00:50.810 --> 00:53.180
these are scripts
that'll do things like

00:53.180 --> 00:54.470
change your background and

00:54.470 --> 00:56.725
change your settings
when you log in,

00:56.725 --> 00:59.900
also to be able to
gather information about

00:59.900 --> 01:03.820
computers and more
administrative tasks.

01:03.820 --> 01:06.690
PowerShell ISE for scripting.

01:06.690 --> 01:09.170
PowerShell ISE is actually

01:09.170 --> 01:10.925
meant to be used for scripting.

01:10.925 --> 01:12.410
ISE actually stands for

01:12.410 --> 01:15.270
>> Integrated
Scripting Environment.

01:15.649 --> 01:19.565
>> This is what PowerShell
ISE looks like.

01:19.565 --> 01:21.830
I have actually already modified

01:21.830 --> 01:23.120
some things to add the command

01:23.120 --> 01:24.830
window in the script window.

01:24.830 --> 01:26.540
If you go at the top,

01:26.540 --> 01:28.850
you can do View and
you can actually

01:28.850 --> 01:31.655
get rid of the script
pane, put it back.

01:31.655 --> 01:33.995
You can change where
the script pane is at.

01:33.995 --> 01:36.275
I could say, show a script
pane over the right,

01:36.275 --> 01:38.860
and there it is
instead of at the top.

01:38.860 --> 01:41.960
I personally like the
script pane at the top.

01:41.960 --> 01:45.785
I feel like it's a little
bit easier to read.

01:45.785 --> 01:48.550
I also like having the commands

01:48.550 --> 01:51.540
added over here on the
right so I can actually

01:51.540 --> 01:53.630
look for my commands and then

01:53.630 --> 01:55.260
your regular PowerShell window

01:55.260 --> 01:57.690
is actually at the bottom here.

01:57.690 --> 02:00.975
When working in PowerShell ISE,

02:00.975 --> 02:02.300
on the script pane,

02:02.300 --> 02:03.770
which is this pane right here,

02:03.770 --> 02:07.595
you'll notice that it
says untitled.ps1.

02:07.595 --> 02:09.860
I haven't really done
anything with it yet,

02:09.860 --> 02:11.885
so there's no name for it.

02:11.885 --> 02:17.830
PS1 is actually the PowerShell
scripting file format.

02:17.830 --> 02:21.770
If you write a file in a
another program and need to

02:21.770 --> 02:25.670
save it as a PowerShell script,
remember that it's.ps1.

02:25.670 --> 02:27.050
That's also if you're
looking through

02:27.050 --> 02:28.970
files and you find
one that says PS1,

02:28.970 --> 02:30.935
you know it's a
PowerShell script.

02:30.935 --> 02:34.100
Here I wrote a script

02:34.100 --> 02:38.725
to get some information
about the BIOS.

02:38.725 --> 02:41.390
What you do in here is you

02:41.390 --> 02:44.060
write your command or
commands that you want,

02:44.060 --> 02:45.544
say, for example,

02:45.544 --> 02:48.530
I wanted to do more than that
and get more information,

02:48.530 --> 02:52.120
I could do Get and then Cim,

02:52.120 --> 02:54.620
and you'll notice the nice thing

02:54.620 --> 02:56.750
here is the
IntelliSense tells us,

02:56.750 --> 02:58.330
"Hey, guess what?

02:58.330 --> 02:59.960
There is a command that matches.

02:59.960 --> 03:01.250
There's actually a couple,

03:01.250 --> 03:03.170
so you can go through here."

03:03.170 --> 03:04.610
If you also notice,

03:04.610 --> 03:07.105
when you put your mouse over it,

03:07.105 --> 03:10.310
you'll be able to
click on it or you

03:10.310 --> 03:12.860
can just use the up and
down arrows to choose one.

03:12.860 --> 03:14.450
When you choose one, at first,

03:14.450 --> 03:16.924
you'll notice you'll get
that nice little information

03:16.924 --> 03:18.380
like you do with
the help command

03:18.380 --> 03:19.970
in your regular PowerShell.

03:19.970 --> 03:21.470
Once you have the one you

03:21.470 --> 03:22.760
want selected, you hit "Enter",

03:22.760 --> 03:24.050
and it will actually complete it

03:24.050 --> 03:27.510
for you. You can see there.

03:27.510 --> 03:35.145
[NOISE] Then whatever
item you want,

03:35.145 --> 03:37.470
if you wanted just
the CPU information,

03:37.470 --> 03:39.615
there'll be a processor.

03:39.615 --> 03:42.770
Here, I'm going to
format it as a list,

03:42.770 --> 03:43.910
so it's easier to read.

03:43.910 --> 03:45.515
I'm going to pipe a list

03:45.515 --> 03:48.535
and then I'm going
to pipe a file.

03:48.535 --> 03:53.400
The first one has
bio2.txt for the BIOS.

03:53.400 --> 03:56.820
I could call this CPU2.txt,

03:56.820 --> 04:00.855
so it pipes it to a
file called CPU2.txt.

04:00.855 --> 04:03.385
Remember, from our
piping to file lesson,

04:03.385 --> 04:06.725
if we pipe anything to a file,

04:06.725 --> 04:08.600
the file doesn't have to exist.

04:08.600 --> 04:10.235
If it does not exist yet,

04:10.235 --> 04:12.080
what it will do is it

04:12.080 --> 04:15.235
will actually create
the file for you.

04:15.235 --> 04:18.620
Once you have your
script written,

04:18.620 --> 04:20.270
there are a couple
of ways to run.

04:20.270 --> 04:22.100
Up here you'll notice that you

04:22.100 --> 04:23.750
>> have a little play button.

04:23.750 --> 04:27.650
>> Then you can
also select part of

04:27.650 --> 04:30.440
>> it and just play the
part of it you want.

04:30.440 --> 04:31.940
For example, I could just run

04:31.940 --> 04:35.165
that by going there
and clicking there.

04:35.165 --> 04:36.770
If you want to run
the whole thing,

04:36.770 --> 04:38.030
you can do the run.

04:38.030 --> 04:40.250
You can also go to File and Run,

04:40.250 --> 04:43.870
or you can just do F5 to run it.

04:43.870 --> 04:46.130
Once you run it,

04:47.650 --> 04:50.600
you'll notice that down here on

04:50.600 --> 04:53.645
your regular PowerShell that

04:53.645 --> 04:56.720
you get a little bit of output.

04:56.720 --> 04:59.555
You see that it ran
that and it ran that.

04:59.555 --> 05:02.360
You won't get much usually
unless there's an error.

05:02.360 --> 05:04.460
If there's an error,
you'll get a big old wall

05:04.460 --> 05:05.690
of red text.

05:05.690 --> 05:07.790
Otherwise, it'll
show that it ran it.

05:07.790 --> 05:11.225
If I want to actually
open up the file,

05:11.225 --> 05:13.280
I can go to the directory or

05:13.280 --> 05:15.560
down here in regular PowerShell,

05:15.560 --> 05:18.260
I can just type in the name
of the file since it's in

05:18.260 --> 05:22.290
the same directory that
we're working with.

05:23.090 --> 05:27.195
I want to open this CPU1,

05:27.195 --> 05:30.390
and notice that it opens for me.

05:30.390 --> 05:34.100
Notice that this script
created the file for me

05:34.100 --> 05:35.510
and it gave me

05:35.510 --> 05:38.780
the information for this
CPU, which is pretty cool.

05:38.780 --> 05:42.350
You can see this can
be super useful if

05:42.350 --> 05:44.420
you're working with computers

05:44.420 --> 05:47.320
you need to get
information about them.

05:47.320 --> 05:51.375
That's pretty much
PowerShell ISE.

05:51.375 --> 05:56.130
Next, you could use
Notepad for scripting.

05:56.130 --> 05:58.340
Some people, they don't

05:58.340 --> 06:00.170
necessarily want the
whole interface,

06:00.170 --> 06:03.440
they just want to write
their script and that's it.

06:03.440 --> 06:06.550
That's fine. You can
do that in Notepad.

06:06.550 --> 06:11.090
What you would actually do is

06:11.090 --> 06:15.880
you would go ahead
and open up Notepad.

06:15.880 --> 06:20.170
Here you can see I
created one already.

06:21.590 --> 06:25.220
You can see I have a
couple of pieces of

06:25.220 --> 06:29.190
information here for a
couple of items to run.

06:31.340 --> 06:35.390
One thing you'll notice is
that obviously in Notepad,

06:35.390 --> 06:37.100
you're not going to
get IntelliSense,

06:37.100 --> 06:38.590
you're not going to
get auto-complete,

06:38.590 --> 06:41.210
you're not going to get all
the fancy pretty things

06:41.210 --> 06:42.410
you do in ISE,

06:42.410 --> 06:44.660
but this is great if
you just want to do

06:44.660 --> 06:48.745
a quick and dirty PowerShell
script and you're done.

06:48.745 --> 06:52.655
What I have here is the file
that I already created.

06:52.655 --> 06:56.180
If I want to create
a brand new one,

06:56.180 --> 06:58.950
I could open up a new Notepad.

06:59.450 --> 07:01.565
When I do this,

07:01.565 --> 07:03.605
I can type in the
commands I want.

07:03.605 --> 07:05.940
I'm going to just get

07:08.270 --> 07:12.190
some information
about the computer.

07:12.710 --> 07:16.590
Again, you notice that
there's no IntelliSense.

07:16.590 --> 07:18.740
Any typos I do or
anything like that,

07:18.740 --> 07:20.150
I have to go back and fix.

07:20.150 --> 07:29.580
[NOISE] I'm going to get
information about the desktop.

07:30.490 --> 07:33.170
I'll format that list too again,

07:33.170 --> 07:35.780
just because I want

07:35.780 --> 07:38.240
to make sure that
it's easier to read,

07:38.240 --> 07:41.270
and then I can actually
output it to a file too.

07:41.270 --> 07:45.590
You'll see here I have a
script that's going to

07:45.590 --> 07:50.755
get information about
the Win32 desktop.

07:50.755 --> 07:52.980
I'll go ahead and save that.

07:52.980 --> 07:58.630
When you save it, you'll

07:58.630 --> 08:00.550
notice that it
normally goes to text.

08:00.550 --> 08:02.320
You'll want to change
it to All Files

08:02.320 --> 08:04.510
so you can save it as a proper,

08:04.510 --> 08:13.475
and I'm going to save this
as mytestscript2.ps1.

08:13.475 --> 08:14.920
Remember, when you save it,

08:14.920 --> 08:16.810
you have to put a.ps1 so it

08:16.810 --> 08:19.840
knows it's a PowerShell
file and save it.

08:19.840 --> 08:23.135
There's nothing you can
really do here in Notepad,

08:23.135 --> 08:28.945
but if you were to go into
your file directory here,

08:28.945 --> 08:31.975
and you'll notice
that it has.ps1.

08:31.975 --> 08:34.090
If you were to right-click it,

08:34.090 --> 08:39.090
you can choose "Run with
PowerShell", and it'll run.

08:39.090 --> 08:41.390
The PowerShell will pop up

08:41.390 --> 08:42.920
>> and disappear pretty quickly.

08:42.920 --> 08:44.845
>> You might not
have even seen it,

08:44.845 --> 08:47.540
but once it's run,

08:48.490 --> 08:54.065
you'll notice we get this
new file desktop2.txt,

08:54.065 --> 08:55.610
which was the file, if you

08:55.610 --> 08:57.905
remember looking at
our PowerShell script,

08:57.905 --> 08:59.675
that we are piping to.

08:59.675 --> 09:03.420
Remember that we've got
information about our desktop.

09:04.160 --> 09:06.560
It's a little more
complicated with

09:06.560 --> 09:08.690
Notepad because you actually
have to go find the file,

09:08.690 --> 09:10.295
run it and all that good stuff,

09:10.295 --> 09:12.290
but you can still
do scripting in

09:12.290 --> 09:15.390
Notepad if that's
what you want to do.

09:15.590 --> 09:18.285
We've looked at PowerShell ISE,

09:18.285 --> 09:19.370
we looked at Notepad,

09:19.370 --> 09:21.920
but there are a couple of
other tools that you can use.

09:21.920 --> 09:23.615
We have Notepad++,

09:23.615 --> 09:26.195
which allows you to color-code

09:26.195 --> 09:29.300
your commands so you can
easily see what you're doing.

09:29.300 --> 09:31.220
You also have Visual
Studio that will

09:31.220 --> 09:33.725
also automatically
color-code your commands.

09:33.725 --> 09:36.950
Visual Studio does have some
IntelliSense items in it

09:36.950 --> 09:42.360
like PowerShell ISE so it
is super useful as well.

09:42.470 --> 09:45.105
Post assessment time.

09:45.105 --> 09:48.290
If you wanted to run a script
you created in Notepad,

09:48.290 --> 09:50.165
how would you go about doing it?

09:50.165 --> 09:53.165
Would you find the
saved PS1 file,

09:53.165 --> 09:55.180
right-click "Run
with PowerShell"?

09:55.180 --> 09:57.660
Would you F5 in Notepad?

09:57.660 --> 10:00.810
Would you go to File
and Run in Notepad?

10:00.810 --> 10:04.635
Or you can't run a script
created in Notepad?

10:04.635 --> 10:06.945
You can go ahead and pause,

10:06.945 --> 10:09.960
and I'll give you a
second to figure it out.

10:11.590 --> 10:14.030
The answer is A.

10:14.030 --> 10:16.130
You'd find the saved.ps1 file

10:16.130 --> 10:18.605
and right-click, "Run
with PowerShell".

10:18.605 --> 10:22.520
B and C are used
for PowerShell ISE,

10:22.520 --> 10:23.915
and D is incorrect.

10:23.915 --> 10:27.064
You totally can create
a script in Notepad,

10:27.064 --> 10:29.755
save it, and run it later.

10:29.755 --> 10:31.965
That's it for this video.

10:31.965 --> 10:33.300
We've talked about PowerShell

10:33.300 --> 10:36.370
scripting and PowerShell
ISE and Notepad.

