>> Hello, everyone. I'm Instructor Gerri Roberts,
and this is PowerShell Scripting.
In this video,
we're going to learn some more commands.
We're going to actually look at locations,
more specifically working with
your current location or your working directory.
We're going to work with get-location,
set-location, and we're going to
learn about push-location and pop-location.
First of all, getting your current location.
When you're in PowerShell, you can
find out what your working directory
is by using the get-location command.
This will display the path of
the current working directory for you.
If you want to set your location,
you can change it by doing set-location.
Now, one thing to note that inside a PowerShell,
you can still use CD, CHDIR,
SL or any of the commands that you normally can use in
the command line prompt to change your location.
Now, set-location will
actually set the location of your working directory,
and when you do it,
the prompt will change to reflect
the new directory just as
CD or any of those commands will.
When you're doing the command,
you do set-location -path,
-path specifies the directory that you're going to.
If you want to display
some more information about the change,
you can use the -passthru parameter switch
at the end so that way
>> it will display more information.
>> Now, passthru may also be used with
some other commands to get additional information.
It's not specific to set or get-location.
You can also save your locations or recall them
>> later using something called push and pop.
>> Push allows you to save
the current location in the stack.
Now, the stack is like
any other stack that you normally work with.
It's first in, last out.
So that means the last location you put in will be
taken off the top of the stack when you go to pop it.
Now what pop does is it takes you back to
the most recently saved directory by pulling
the top location off of
the stack and then bringing you there.
Now it is time for a demo so you
can see what those commands look like.
Now let's take a look at
>> some of those location commands.
>> The first one is get-location.
That's pretty much just going
>> to give us a current path.
>> So get-location, and you can see
that currently
our working directory is C:\WINDOWS\system32.
Now this might be important because some commands
actually are impacted by the current working directory,
so you want to make sure you're in the right place.
Now, the next thing you can do is set-location.
Now, set-location will allow you to set
the location to a place you'd like to go.
It's very similar to the way that
CD works in that if you were to
say cd to c and then Windows,
you see how you change into Windows.
If you were to cd back
into C:\Windows\system32,
you would go back to system32.
When you do set-location, it's pretty similar.
What you do is you do set-location,
and then you do the path parameter,
and then you do the path.
Here, let's go ahead and
do c:\windows like we did with CD,
and you can see that it does a very similar thing.
Now, if we want to go back,
we can do the same thing and just put
our system32 path. There you go.
So that's setting location.
Now, another thing with
set-location that we talked about
was the passthru command.
Now you noticed when we were setting
the location in these two other spots,
we didn't get a confirmation.
The only thing we got was that
the prompt change the directory.
If we want some sort of confirmation and information,
we can use passthru at the end.
Look at that, we get
an actual path and it shows us where it's going to go.
Lastly, we want to look
at saving and recalling recent locations,
which are push and pop commands.
What we're going to do is we're going to get
our current location to see where we're at,
which we know is in system32.
Then we're going to change our location
by going ahead and
pushing the current location onto the stack.
So we do push-location path,
and then we're going to move to the local settings
folder by doing a path in local settings.
Now, push works like set-location,
but instead of just moving,
what it does is it pushes your current location onto
the stack and then goes to the location you tell it.
Here, we're going to go ahead and
push the local settings to where we're going to go,
and then we're going to save the system32 in our stack.
Now notice I got an error.
That's actually because there is
no local settings in system32.
Your local settings folders are
actually in your users folder.
If you put the wrong path,
it's not going to find
it and you're going to get an error,
and it's just going to stay in the current path.
Now we can also try maybe going to a user.
Let's do actually cd
c. If you want to see what's in a directory,
you can do dir for directory,
and it'll show you what's in there.
Now, if you see, we
have a couple of different folders here.
Let's say we want to go find users.
We can go cd and then users,
and we are in users.
Now we can see what's inside of users.
We have a couple of users.
Now this is where we're going to go ahead and
do some push and pop.
We're going to push the current location onto the stack,
and then we're going to move to my folder.
Notice now that I am in my folder.
Now we're also going to change
again so we have another location in
the stack because we're going to try to
build up the stack here.
I'm going to do directory to see what's in there.
You can see all sorts of stuff,
but we're going to go ahead and
go to the documents folder.
We're going to push-location.
That's going to save our current item in our stack,
and then we're going to
path and we are going to documents.
Now we're in a different location.
Next, let's get
our location and we see we're in documents.
Now we're going to pop to the last location
we are in with pop-location.
You can see it there,
but you can also do get-location to see where
we're currently at. It's kind of cool.
You can save it in the stack and then go back.
You can keep going back to
all your saved locations
until you hit the end of the stack.
That's useful if you're working in
multiple locations and want to go back and forth.
But just make sure
that when you're pushing and you're popping,
that you remember where you're
at because it's easy to get lost.
But this is also an easy way to have the ability to
go back and forth between
different locations in set-location,
or CD, or CHDIR,
or any other things like that.
That's it for our demo for locations.
Now it's off to quiz time.
In this quiz, we want to know if you wanted to get
back to the most recently saved location in the stack,
which command would you use?
Would you use push-location,
set-location, get-location, or pop-location?
I give you a minute to figure that out.
You can also pause if you want,
and then we'll give the answer.
All right. The answer is D, pop-location.
Remember, pop-location allows you to pop
that last saved location in
the stack so you can go back to it.
This is due to the stack being FILO,
remember first in, last out.
It's like a stack of books.
The last book you put in you can grab.
That's it for this video.
In this video, we talked about locations.