PowerShell for Beginners
By Automate with Rakesh
Summary
Topics Covered
- PowerShell Automates Every Windows Admin Task
- PowerShell Commands Always Follow Verb-Noun Structure
- Stop Trying to Memorize PowerShell Commands
- Pipeline Filters Find Large Files Fast
- Try Catch Finally In Plain English
Full Transcript
welcome to Powershell tutorial to learn Powershell the very first thing you do on your Windows computer simply type Powershell by
default you should have Powershell on your computer click on it let's see what is the version of Powershell that is
currently available on my computer for that the command that you have to type is dollar which is there in your keyboard
4 and then type PS stands for Powershell and then version table this
is the table dot PS version Powershell version this is the command once you type the command hit on enter and see
what is the current version of Powershell script or the Powershell you have on your computer I have 5 1 22 621
at the moment let's go to the browser and type latest Powershell
version hit enter I can see a page from microsoft.com and the Powershell version
that I could see on the homepage is 7.2 let me open this and you can see it says Powershell 7.4 long-term support that is what we
have and on this particular page you have a MSI package click on this MSI package on
this page you can see Powershell 7.46 based on the time you're watching the video there could be a higher version here on this
page all you have to do simply click on the MSI package based on the version of your computer so I have a 64 version of
os so I'm going to use the first link if it is 32bit you can go with the second link now in the download section allow
for some time for it to download Once the download is complete I'm going to click on
this and this is the screen that you would see welcome to Powershell 7 x64 setup I'm going to hit on next it is
going to select the default location of your C drive C drive program files Powershell if you want to install it elsewhere you can select it I
would recommend is to leave it as it is and I'm going to hit on next and these options the default
option leave it as it is and hit on next and all this default recommended options do not change them leave it as it is and I'm going to hit on next and
install it you might get a prompt for user control click on yes and allow the installation to
complete you can see on my screen the installation is complete now you will get this particular screen and here you
have the option launch Powershell click on launch and hit on finish and you can see the power cell has opened and you can see the version
number is now 7.46 previously we tried a command we can also try the command just for
learning purpose PS version PS version table. PS version Powell version hit
table. PS version Powell version hit enter you can see it says 746 that is 7.46 so the installation is now complete
now the next thing that comes to our mind what is is the use of Powershell Powershell is a powerful task Automation
and configuration management framework from Microsoft let's see some key you usage
of Powershell automating administrative tasks Powershell excels at automating repetitive tasks on Windows systems like
managing user accounts groups in active directory configuring network settings managing services and processes working with files and folders
scripting Powell is a full-fed scripting language allowing you to write complex scripts to automate complex tasks Remote Management manage multiple computers
remotely using poal remoting Cloud management cicd data manipulation so there are lot many usage slowly we will
see how power shell can be used the next question you may have why Powershell does not have ISC what is
isse integrated scripting environment what is isse integrated scripting environment Powell 7 does not have an
integrated scripting environment or is for few reasons what are the few reasons architectural differences Powershell 7 is built ONN
net core while Windows Powershell which includes the ISC the windows Powershell has a ISC integrated scripting
environment but Windows 7 is not because this is built on that is built on net core Powershell 7 and the windows Powershell is built onet framework so
what is the difference this makes it difficult to integrate the IC which is tightly cou with NET Framework into Powershell so what happens the Powershell 7 is more focused on the
crossplatform compatibility Powershell 7 aims to be crossplatform running on Windows it can
run on Mac OS Linux right so that's why it doesn't have a isse or called integrated scripting environment so if I don't have a
integrated scripting environment then where do I code so Microsoft recommends Visual Studio code or vs code
with the Powershell extension so now you need one more tool to be installed that is called Visual Studio code or VSS code
now let's install Visual Studio code for that go to your browser and simply type Visual
Studio code enter so the very first link that you would find from code. visual
studio.com click on this link and here you would find download for Windows I'm going to click on this and here you have the option
download and here you see Windows 10 11 I'm going to click on this and you can see the installation the download has already started give it
a few seconds all right so the download has complete I will go to the folder and try to open it so here you can see the setup window has
appeared all I have to do I'll click on I accept the agreement hit on next then I would leave the default location as it
is hit on next uh already exist would you like to install uh to that folder anyway because I had already installed it last time that's why it is prompting you you might
not get this prompt that's okay simply go and follow the instructions on the screen Visual Studio code hit on next and leave all these options as it is no
changes and install so you can see the installation has started give it few minutes all right so the installation is
complete all I have to do hit on finish after you launch Visual Studio this is what you would find choose your theme and all these options you would find I
would like to leave it as it is the default one so what I will do I'll simply close this window if you're interested in different ones I like the
dark one I will leave it as it is the default one I'm going to close this window then the very first step is to
install the extension and what is the extension we have to install for the Powershell you simply type Powershell here in the
extension so this is the button okay extension button I've typed Powershell click on the very first
appearance this is from microsoft.com okay this is from microsoft.com and if I read this this extension provides Rich Powershell
language support for visual studio code now you can write and debug Powershell scripts using the excellent ID integrated development environment
like interface that vs code provides so this is what you need to do your power cell scripting for Powershell 7 all right so what I will do simply hit
on install and the installation should complete in matter of few seconds just wait for a few seconds okay so you can see the
installation is now complete okay this is complete you can see if I click on this um
extension it is all enabled and all set all you do simply close this okay I'm going to also close the welcome so everything is closed now what you do go
to the Explorer on the the very first icon on the top this Explorer will have something called open folder now click
on open folder and you can save your code wherever you like I'm going to click on documents where I would like to keep all my Powell
scripts let me create a folder here right click new folder and I will name it as Powershell
scripts okay so this is the folder I want so click outside highlight that particular uh folder and then hit on select folder that means you'll storing
all your scripts that you are going to learn in this particular folder that's all the meaning is hit on select folder okay so once you're done do you
trust the author of the file in the folder it ask uh trust the author of all the files and parent folder documents I will check it okay and then I'm going to
click on yes I trust the authors done so you have selected your folder this welcome screen is coming out
I will close it now what you do here in the Explorer if you bring your cursor you see the folder that you have created currently the folder is
blank now all your power cell script so how do you create your very first Power Cell script I'll show you so what you do click on this particular button which is
new file and then give it some name I will say powerwell script test 1. PS1 1 so
what is PS1 this is your extension that you have to write you have to save all your power cell script files with the extension do PS1 and hit enter so what
happened the file got created let's do one thing let's visit that file okay just to see I'll go to my documents and uh the power cell script
file and here you can see PST test one.
PS1 this is the file just now I have created which is currently blank by the way now let's write our very first code okay
so I'll teach you how you have to write the codes and everything but let's take with very simple one get date so get date so what is get date so get date is
nothing but you would like to run and understand what is the date today so here there's a run button okay I'm going to click on
this and on the ter you can see the output has automatically appeared 17th November 2024
84920 you're getting it so you got the output so this is the command let's understand how commands are written in Powershell in Powershell commands known
as command LS very important jargon the commands are need known as command lets pronounced as command
lets and they follow a very very specific structure what is the structure the structure is vve hyphen
noun we just saw get date so verb like Get Set add remove these are the verbs and the noun date is a noun
process is a noun service is a noun item is a noun so it always follow this structure VB hyphen noun remember this and this are known as whatever you are
writing get hyphen date is a command L let us learn one more command L called get
services on my screen let me open task manager and here if you see there are multiple processes which are currently running on my
computer now is there a way or is there a command to get all the service needes so let me try get hyphen we will follow
the same syntax the first is a VB hyphen and then a noun so I want what services so I'll say get
Services get service and hit enter and then I'm going to run this so if you run this look at there are two buttons here one is run if I run this
what happens the entire everything that you have written will run so for example you can see the get service has given me all the different Services which are currently stopped running and everything
right all the services it's shown me let's say I'm try trying the other command get date okay Tab and then I'm going to run
it now in this case what happens when you have two different command LS so what is happening the First Command ran and the second Comm run so both of them
have run and it shown some output so it has shown today Sunday 177 and XYZ things it has shown everything now in sometimes you are not interested to run everything rather you
are interested to run any one command for example I want to run only this command so what I will do I'll simply highlight this particular command and that out of this two option I'll use the
second option let me show you when you highlight this it says run when you highlight this it say run selection so if I simply run selection only the code
the command let which which I have selected has run and it given me output 17th November 2:22 p.m. okay so this is
how the output will appear as we have got few idea around the command LS let's understand the vs code editor slight a
bit on my screen if you see this is how the VSS code editor is appearing on the left side if you see the variable watch call back from where it is coming this is from this one if I
click on it it's getting hidden if I click once again it is appearing okay from this debug panel now the other thing for example it is something like
this you want to you don't want to see this this also another option toggle primary sidebar so if I simply click on this once again you can see that is
hidden there is also another icon here if I click on this icon what happens the terminal window is gone if I click once again on this icon this one is appearing
you getting it similar way this one side this for the bottom the symbols are there and this is for the right for example if I click on this the right window is
appearing if I click on this again it is gone this is like this layout these are just layouts okay if you click something you have clicked by mistake again click
on that it'll go out so however you want let's say I don't want to see the terminal I'll simply remove it so what would happen if I run it the terminal will automatically appear I don't want
to see it click on it again it's gone the next thing the next question that comes the text is very very small I want to increase the font size of the
text how that can be done for that I'll click on file I will click on preferences and here there is something
called setting things and here if you see there is there is option here under the text editor if you click on
font this is what will appear and there is something called font size so I will keep it to because I'm teaching on the screen I want slightly a bigger number so I'll put
maybe 33 click outside and if I go back to my script you can see the font size of the command LS has increased the other thing I am also
would like to say sometimes you would like to apply a theme now I'm using a dark default
theme there's option here file preferences and then I would go to theme and I'll click on color theme so here
these are the default options you can try out the moment you click on something for example I'll click on this you can see how it has changed so based
on your need you know you can think what you want how you want so all these things you can think of you can try out few more
file preferences theme color theme uh let's say let me
try uh monai dimmed red so there are many many options see how it appears so it's up to you how you would like to keep it go file
preferences theme color theme I'll put it back to default dark mod there's also another option file
preferences theme color theme um so you let's say for example there is something called uh you can also use browse option here
browse additional color themes and um searching for themes let me type let's say Dracula theme you can see different themes are available it's
up to you you can play around on around this options based on the theme that you like you can keep it okay so I'll go
back to this one so these are couple of theme option try out but do not try out many things and then you know you entirely are not aware how to get back
so be very very careful now the next question is is there a way to remember all the commands the answer is you don't have to
remember all the commands I'm going to show you two alternative ways to find all the commands all you have to do simply
type a command called get hyphen command this is the command let you have to type get hyphen command and
if I run this it is going to show me all the different commands that that is there in pow shell start job start process start service start sleep you
can see with different vs remove variable uh remove BCD entry bits transfer right get history get host get
item get job so like that you have hundreds and hundreds of command lists so I don't have to remember now for example what if I forget do I need to go through
all this command lists and find you don't have to so I'm going to show you another easy way all you do simply type get
hyphen command space and I'm going to use hyphen and let's say I remember the noun so the command lets are formed verb
hyphen noun so I'm going to say noun and let's say I remember the word called service I just remember the word service but I don't exactly know what to do with
the service so I'll simply type that and if I run this what happens if you see all the things that are there with service for example get service new
service remove service restart service resume service so all those nouns will automatically come and the same thing you can do with also wob for example if
I type hyphen vob and I remember it is something to do with let's say install I don't remember the exact command something to do with install so if I
simply run this selection you can see all the things will come install DTC install module install script right this
way it quickly helps you to find the right command sometimes what happens for example I need to understand how to
use uh let's say install script okay or install package so what you can do simply we say
get hyphen help and use that command copy paste the command uh command lit and then write hyphen full so this is
what I want if I run this selection okay so here if you go out it will say the input required is system do string output system do U object so like
that it will give you certain help around that command now let's understand what is an alas to understand alas you remember the
previous command how to get help so you write get help and let's say I want to get help on service and I will simply
say full so this was the command we just learned if I run this command if you go up there is something called Alis what
is this gsv means let me show you when when you type get hyphen service and then when you run this particular line what happens used to get
all the services now the same thing can also be achieved using the alas for example I'm typing gsv that was the Alias for get
service so if I type this and I'm going to Simply run this line what happens you can see the similar answers have appeared using GS instead of get service
now if you are an expert if you remember this alysis then it's becomes easy but it might be difficult for others who
might not remember the alas what is ESV so how do I get all the list of ales so simply all you have to do simply
get alas if you type and then run this particular line of code you can see all the eles that are there within Powers shell has appeared for example example
move item you can write MV new alas you can say Nal right for example uh uh stop process kill right these are the
different alyses that can be used so as a beginner you should uh slowly learn to use this Alis but again when it comes to code readability I would not recommend
you to go for Alis rather use the right full the entire command L as a program the next thing that you should learn is about variables so before you learn
variables you need to be aware of the naming Convention of the variables so basically there are three important ones
one is the camel case and the other one is the Pascal case and the other one is
snake case this so these are the three different variables so how does the camel case looks like for example I want to declare a variable I will say my
variable so the very first ward of your variable the very first character of your variable starts with a lower case it's more like a camel you know there's a hump and the next word onwards you
would have your capital okay Capital words my variable let a string so you can see the very first character I have kept it as lower and the rest becomes
Capital the next set of words okay so this is how you do a camel cas if it is a pascal case you write everything every first character of the
word starts with a capital so I will say my variable and if you start with the snake case probably I will say my underscore variable it's more like a
chain like a snake so this is how these are the different three cases you can think which one you like and you can go with I will go with the Pascal case
Capital everything the first word I will start with the uh Capital letterer so let's see how you declare variables in
Parell so let's declare a variable to declare a variable you'll use the dollar symbol and you will write your variable so I'll say my variable equals to I'm
going to write my channel name automate with rakes now how do I print this variable
again use a dollar symbol and use your variable name we can hit on enter now the variable is
declared let me run it so if I run it you can see here it has printed automate with raakesh now sometimes what happens if you have too many things on your
screen you can simply type clear and hit enter so that everything will be cleared let me run it again you can see the output here has been printed automate with Rakesh
sometimes you can also go with single code but ensure it also ends with a single code if I run this even it is going to
print the output now what if I'm going to remove this and within single code let me type
a 06 if you're putting anything within a code that will be called a character you can see it has printed even 06 as a character
now what is this character this is called a string so a series of characters is called a string right now if I type without a
quote so this becomes a number for example I'm typing uh okay 06 so this becomes a number if I run this you can see the way it has printed last time it
was a character it has printed 06 because now it's a number there's no meaning of keeping a zero before the number so it has printed six
now you need to be aware of few things which are called Methods and properties if I put a dot here you see the wrench
like a symbol which comes these are called property so if I go with this let's say I'm typing length if I run
this what is the output the output says two so that means the character length of the variable is two so that's a
property now let me use a DOT again and instead of the property I'll use the cube symbols what are this Cube symbols these are called Methods this is called
property and the cube symbols are called Methods so I'm going to use one of the method called get type so the moment you type you will see the parenthesis has
automatically come let me close the parenthesis and if I am going to run this it'll tell me what kind of a data type it it is so it said the data type
of this is a string if I remove the code and try to run it you know the data type is now on
Integer let's do one thing let's declare few variables so I've selected my variable one I'll type one the first
variable and let's write few more variable so this I will say as let's say five now what would happen if I use a
dollar symbol and I will my I'll add both of them my variable 1+ dollar my variable okay I did not
declare so change it to two and then say my variable two so when you Summit and run it definitely I'm
expecting an output called 11 you can see it has come this is one way there is also another way I can use a dollar
symbol and I can say my variable um let's say result I will say equals
to and I want to print dollar my variable result so if I'm going to run this we
can see the output has come 11 so these are the some some different ways of using the variables now let's see some basic arithmetic operators so the
plus one you have seen for example if I have to do the minus simply write minus symbol and run it and you can see the output has come one sometimes it's good
to clear the output let me run it again you can see the output has come one this is for substraction let's say have to do multiplication use the star symbol
and run it and you can see 30 has appeared let's say I would like to do the division use the division symbol run
it and you can see it has come 1.2 6 / by 0. uh 6id by 5
by 0. uh 6id by 5 1.2 uh now the reminder right if you want to get the reminder this is the modular symbol you have to use this and
if I run this you get reminder as one when you divide 6 by 5 reminder is one next let's see how the Powershell
Boolean variable works now for this let me create a Boolean variable I'll use again remember dollar symbol my
Boolean creating a variable name and here I'm using a equals to symbol and what I will do I'm going to assign some value to it so Powershell has this
values which are the default values dollar true dollar false I'm going to hit on enter now let's see what is the type of this I've created a variable but
let me see the type so for understanding the type you will use the dollar symbol type the variable name my Boolean
variable and once you have typed it dot the method that you will use is get type right this is the method you get the parenthesis you close it now if I only
run this the selected option you can see the output has come bullan similar way you
can also say false so for example I can say dollar false let me run the selection by
selecting the code run selection and you can see the um uh it's a Boolean type okay so the data type is a Boolean type
what I'm trying to print all right now what why are we writing dollar false let me tell you for example if I'm going to
create a variable with the same true or false right for example if I type true equals to 1 what happened you get an
error if you highlight the error it tells you the variable true cannot be assigned since it is a readon aut automatic variable that is built into
Powershell please use a different name so it will not allow you to use a variable or the value which is already
set for Powers ship you can't use this as variable so this is about the Boolean variables now let's learn some basic
comparison operators I'm going to delete everything here and let's take some two numbers for example if if I have to say
2 equals to 3 what should be the output it should say false so I'll say 2 hyphen equals to 3 so this is how in Powers
Shield it is written now if I'm going to run this you know the output which it has given is false now 2 is equals to 2
if I write the output is true now let's do one thing let's quickly learn all the
comparison operators one by one so you understood this is how the equals to is written in Powershell hyphen EQ equals to pretty simple easy to remember and this way it will not work in other
programming languages you might use equals to equals to right so if I run this see it doesn't recognize okay so
let me clear it and minimize the terminal so how you write equals to hyphen e QBE simple similar way if I
have to write not equals to then how do you write similar way hyphen not equals to NE e so easy next for example if I
want to write greater than so 2 space hyphen greater than GT greater GT okay
three okay if I have to check 2 uh is greater than equals to so hyphen greater than equals to
3 if I have to check less than equals to 2 hyphen less than uh less than if I want to check less than three so this is how you write let's say
two uh less than equals to high less than equals to 3 getting it so these are the important one equals to not equals to greater than greater than equals to less than less than equals to some the
basic comparison operators if I run the code you can see the output what is the output 2 = to 3 it said false 2 not
equal to 3 that is true 2 is not equal to 3 2 is greater than 3 false it's not greater than three now it will check 2 is greater than equal to 3 it'll say
false right 2 is less than three is 2 is less than three yes true 2 is less than equal to 3 True right so this is how these are the some basic comparison
operators in poell next let's learn about the array in power cell let me erase this code and create an array so
what is an array so array is a data structure which can hold multiple items for example I created a variable a and
in that I will store one 2 3 4 5 so what is happening now this variable is now can hold multiple
different items now for example if I want to check whether it's an array or anything else
all you do dollar a do get type close the parenthesis now if I'm going to run the
code what is the output it says okay the variable a is a array and it contains the integer kind of
data now I would like to understand let's say I would like to print or maybe let's understand how many characters are there there is another
property that you can use called count you remember this rench symbol that you get they are called properties and the
cube symbols are methods so here I said count now if I'm going to run the code it will tell me the output is five so
total there are five items 1 2 3 4 5 now the next thing we have to learn in the uh uh array is that if I have to
print any number from the number of items so there are five items let's say I would like to print the very first item so if I write the index number zero
so the indexes of an array always starts from zero so it will be 0o index index means position so 0 1 2 3 and 4 so there
are four positions the number starts from zero so if I print like this and run the code what I'm expecting it has printed one now if I
write one here what it is going to print it should print two in the position number one what is there two is there so if I run it you can see the two has been printed
now let's say I would like to print from the index number zero to there is a range operator like that you have to put two dots and I'm going to say three so
that means what would be the output zero means it will print 1 2 0 1 2 3 so until here it will
print until the third position so you can see 1 2 3 4 it has printed four numbers similar way if you want to
create a range for example I'm saying 1 dot dot 10 and I would like to print the array simply I'll use the variable and run it
so you can see what it has done 1 2 3 4 5 6 7 8 9 10 it has printed everything one only thing you have to remember is the range operator two dots you have to
use okay now a couple of other things for example if I'm going to print from the zero position uh uh and then I'll put two dots and
I'll say print it till the fourth position so what I'm expecting when I run this I'm expecting 1 2 3 4 5 it has
printed you can also use the negative uh numbers for example I want to print um let's say the opposite way I want to
print from uh let's say the ninth position until minus 5 position so if I'm going to run this you
can see it has printed what happened the countdown has started um so in the opposite way uh so it'll be more easy if
I put like this one comma 2 comma 3 comma 4 comma 5 Okay so let's see I'm going to use two
different operators I mean values here so I would say print from minus 2 and Min
-3 okay so if I'm or minus 4 let's put now if I'm going to run this so what happened what got printed let me clear
it okay let's run it so 4 3 2 what has printed 4 3 and 2 so what are you understanding from this 4
32 so this is the in in in opposite direction it will run okay so -1 -2 - 3 -4 so it has printed
from 4 3 2 in opposite way it has printed see minus 2 is this one and then it has printed till here okay so if I do
opposite for example I say -4 and I do -2 minus sorry minus 2 so last time it has printed 432 okay now if I run it
what it has printed 2 3 4 so so - 4 what is -4 -1 - 2 - 3 - 4 so this is the
position and then 2 - 1 - 2 so it has printed from here to here 2 3 4 if you write opposite way it has printed 432 so these are the couple of uh functions or
ways of dealing with array that you have learned next let's understand the for each looping construct so how the for each looping construct
Works let's create a variable or array variable so I will say dollar a I'll create a variable name called a equals
to and I'm going to say 1 2 10 so that means when I'm going to print if I have to print um the dollar a and run the
code what happens see one everything got printed 1 2 3 4 5 6 7 8 9 10 printed it is printing now this is can be done if
you want to Loop through you want to Loop through an array how will you do it now what is this for this you I'm going
to use four each this is the looping construct for each so wherever you find this Cube symbol you know it's a method
so for each so for each I'm going to use and how it is written pretty simple use parenthesis symbol and inside that I'm using dollar I it is
more like a counter variable in dollar a so what is dollar a here dollar a is the one which has the value
one till 10 so it will the when it Loops through the first value will come to I and we need to print it for that all you
do use two curly braces hit enter and inside this I just want to print dollar I that's all so dollar I see pretty
simple construct for each dollar I in dollar a so the very first value will come here now let us first run it and see what is the output comes so what is
the output 1 2 3 4 5 6 7 8 9 10 it has printed now what we can do with this let's say whatever value it comes I should multiply by two so what I can do
into two now if I run it what would happen we see the first value came 1 so 1 into 2
is = 2 2 into 2 is = 4 3 into 2 is = 6 so it has got the multiplication so now I'm looping through the entire array for each element for each element I'm
looping and I'm doing whatever wanted within the loop now the next thing that we are going to learn is a hash table so what is a hash table so hash table that
you hear is nothing but a dictionary in many programming languages you call
it as dictionary so what is a hash T in Powershell or a dictionary let's understand for this let me create a variable how do you create a variable
always remember you're using the dollar symbol the number four and then you write the variable name for example I'm going to write the
variable name called settings then I'll say equals to then when it comes to creating a hash table or any kind of a collection
collection means multiple values you would be using something called at the rate and then curly braces and inside the curly braces you define
your set of data so a dictionary contains a key and a value
so what is it for example I'm going to say I'll create a variable so how do you create a variable in double code I'm going to create a I mean I'm going to
create a key called app name and I will say equals to and again in double code I'll say app one so this is one key
called app name and this is the value called app one okay hit enter and the same thing I'm going to create double
code let's say I'm going to say version and here I will use equals to symol and say I'm going to say
1.0.0 this is the version number and let me use another uh key called Max users how many Max users can
log into that application and here I'm going to say 100 so what I just created is a hash table
so a has table has a key and a value a key and a value okay so it could have multiple keys and multiple values so
what is unique about a hash table the unique is the key you cannot have the same key twice for example the left side are the keys if I'm going to
type version once again and you can see it has started giving error what is the error duplicate
Keys version are not allowed in the hash literals it will not allow you to have duplicate keys so the keys in a hash
table or a dictionary are always unique so what are the things we learned we created a variable in generic way like dollar
settings and then at the rate at the rate is used to define the number of collections that means you can have multiple items inside that
variable and then you use a two curly braces within that you define your key left side is a key then use equals to symbol and on the right hand side you
write the value and there is no comma used anywhere pretty simple so this is how you create a hash
table now the question is okay once the hash table is created I would like to fetch some particular value how will I do it so for this again
use the dollar symbol and use the variable name and to retrieve any value so when it comes to collection you remember you
we use a square bracket and then write the index number in the array but in a hash table you'll be writing the key so
for example I would like to know what is the version or I would like to know the application name so all you do simply write app name now tell me the app name
was declared like this and I'm writing like this is it K sensitive no so if I run this also it is
going to work so you can see the output application one has come let me clear it and run it again you can see
application one has appeared so this is not K sensitive however you like you can type and the output app one has appeared
similar way can I print two things at the same time possible so all you do put a comma not recommended but if you want it
is possible so I'm going to say version then I'm going to run it you can see what the output came app 1 and
1.0.0 two things have come okay let's minimize the terminal portal so how to retrieve something we got to know let's say I would like to change
the value for example the version is 1.0.0 I would like to make it to 2.0.0 how will you do same way first you need the dictionary name
dollar settings uh sorry the hash table name and then within the square bracket you will write which what you would like to change so I would like to change the
version for example I would like to change the version is equals to in the double code I'm going to say
2.0. through. o. now let's say if you if
2.0. through. o. now let's say if you if I'm going to print now dollar take the has table name and you like to print the
version so what would happen the has table ver which is here now it has been updated with 2.0.0 the output should I expect is 2.0.0 you can see the output
that has appeared is 2.
o. getting it so this is how it works now the next thing what if I would like to Loop
through the uh entire uh hash table how would you Lo through so pretty simple you remember to Loop through we were
using the construct cor for each for each and then give a space and use parenthesis that's
all so here what you use to write dollar I which would represent is more like a it will contain the value while it Loops
so dollar I it's more like a container variable dollar I in dollar your dictionary or your hash table variable which is
settings dollar I in dollar settings that's all right and and then once you write it what you need inside the parenthesis you'll write whatever you
want so let's say if I'm going to print dollar I okay I'm going to print dollar I so what would happen what is getting printed let's first understand
so this is my hash table and this is how I'm using the 4 construct and I am looping through the entire dictionary and I'm printing it
here now if I run this what happened the entire dictionary got printed so you can see version 1.0
Max user 100 app name is app one getting it so it has printed the entire thing the multiple ways you can play with this so dollar I do
settings this is one way you can also do something like this for example I will say keys dollar I keys this time it is only going to print the key
name if I run it you can see it has only printed the key names version Max user app name okay similar way if I simply
say dot values it is going to print the values only the values okay so multiple way however you
like you can play for example you can also pass this way for example I'm passing dot Keys now if I'm going to print only
dollar I I is going to only print the keys you see okay it has printed only only the keys now let me
minimize now let's say I want to print the values how will you do it I'll use the dollar settings the has table variable
and within the square bracket I'm going to say dollar I so that means I'm printing the keys here so if you print the keys what would happen the values
will automatically get printed so you can see the values got printed okay so like there there are multiple properties of a has table that you can explore uh for
example I'm using dollar settings dot uh there is something called let's say contains key
so what this will check it will tell you whether a particular key is present or not so close the parenthesis and within double code I'm going to say for example
ease version is present inside this hash table if you run it the output it says true right so however you write it doesn't matter for example I'm writing
Capital version also the output will be true okay so it's not Cas sensitive when
you you are taking the variable names in Powershell all right now if you type let's say I'm typing version something like that then it will say false it's not present so you have to be the
spelling has to be correct the character has to be correct okay so like this there is also uh let's explore few more methods dot uh there is something called
also count okay so if you use this you can see it says three so there are three pairs so one two and three three paars
so this is more about your has table in Powershell now the next thing that we have to learn is a custom
object so what is a custom object and how that works let's see it let me use a comment here saying create custom
object now how do you create a custom object it is more similar how you have created a has table again let's write dollar symbol
and create a variable called person equals to and the custom object when it comes so what is custom object we'll understand we need an example to
understand it so let's see the syntax first so you have to use a command L called PS Power Cell script custom object this is the command let you have to use the moment you select
automatically the vs script is going to give you this structure so it has two things name and then you have to write value in has table your key were quoted
in double quote but here this won't be quoted the value can be because if it is a string you have to quot it now for example let's say I would like to store
the first name of a person and the value I am going to keep it for example jaw okay hit enter now the second thing
uh let's say I want to keep the last name so last name see I'm not using double quote like has table every key used to be always in double quote because they were always strings now
this is more like a property of that object a person has a first name a person has a last name this are the property of a person all right these are called objects so object will always
have a property for example car car has a property red color write white color black color they are properties similarly so these are called objects so object has a property and you don't have
to double code them okay lasting for example I'll say to let's say I'm going to create another property called age and I'm going to say
30 and let's say I'm going to say occupation equals to and I will say software
developer so what you see now here is a custom table a custom object which has a property and its value a property and its
value okay if you compare between the hash table previously what we learned the only difference that you would be able to see how it is written is it has
something a command L it uses called PS custom object and then your variables right the properties are not double qued they are simply written like this here the keys
are double qued now let me see what is the use of this let's see custom object why do you really create for example I have created
a custom object called person I'm going to use the dollar symbol and I'm going to say person dot now the moment you put dot what happens the of the properties
that you have created let's say age you have created first name you have created last name you have created occupation you can see with this setting symbol all the properties can be accessed I can
access those properties for example if I say age and going to run it what would happen the age 30 got printed let me clear
it for example minimize the terminal let's say I'm going to use another property Dot see the moment you put a dot it
automatically appears for you and run it so you can see John has appeared now let's say I'm going to tell you print his first name and last name
together how will you do it now for example I want to print the last name and first name to so I will say in double code I'm going to
say full name of the or simply say full name okay full name colon give a space and I want to print it now what is the problem here
see the DOT first name when you put a double quote it's not working you can see the method is not working now for this when you have to print within a string then what you do you use another
dollar symbol and use a parenthesis around the object do you see now you can see the
color has changed now when you print it you can see the full name full name just now John has printed let's say I want to print the last name
also so what I will do I'll give a space and then use the similar method dollar parenthesis and again call the object so
the object name is person dot last name see the properties automatically appearing for you now now if I'm going to run this you can see the output has appeared John
do now what is the differences you are able to make out of this here the PS custom object so you have created a custom object with multiple properties you can add as many
properties you would like first name last name age occupation anything else about the person you can add you can continue to add them and this things can
be accessed using a DOT all right now for a hash table you can't access something like this right for a hash table how did you use it for
example dollar settings dot if you put it doesn't give you any of this this can be only accessed using a square bracket and
inside that you use to write for example if I type version then you will be able to access the values so for example if I run this you can see 1.0.0 has appeared
but where in a custom object you can simply access the value just by using a DOT first name so these are some of the basic logical differences between a custom
object as we have learned how to create custom object let's see how you can create a list of custom objects so what
is the list of custom objects let's see it for this I'm going to remove the entire thing and put a comment list of
custom objects okay now for this if I ask you to create a list what you do you create a variable with a dollar symbol so I'm
going to say employees as a variable and then use equals to symbol and use atate symbol and two parenthesis so this is how you create a list list means
multiple objects in one variable multiple values inside one variable so I've created dollar employees now here I
have to create custom object that is to list of custom object that means multiple custom objects so for the for
this first thing what you do write the PS custom object command L okay the moment you hit enter everything will get automatically written for you this extra Square symbol
remove it so now let's use something so so I'll say name I'll give a name let's say Alice and then I will create another one
called H another property called age I'll write let's say 45 and the third one I will say Ro equals to is a
manager okay this is one object I have created now when I see the code like this it doesn't look really so good okay I want to keep it at one line because I
will have multiple objects so what I will do I'll Del put keep my M mouse cursor here click on delete delete delete delete delete and
bring it up here similar keep the mouse poin delete delete delete you know keep the here and then again keep my mouse cursor and delete delete this is how I
have got everything into one line the moment you bring it to one line what happened you see you started getting some red you know it's it tells you
there is some error so what is the error so when you bring it in one line all you do simply use a semicolon semicolon here semicolon here
now you can see all those errors are gone this is a list of objects so what I will do I'll copy this entire line bring the cursor here hit enter and
paste it once again some three times all I will do I change the names here for example here I'll say
John whose age is let's say 25 who is a developer and then I'll use another name called Bob whose age is let's say
28 and he's a tester so now what you see is a list of custom objects okay now the important part once
you have created this list how do you iterate through them that is important so next thing we have to
laar iterate through a custom list of custom objects okay list of custom objects so how do you iterate
through now for this I'll use a for each construct and you know how to use the syntax parenthesis and I'm going to use
a dollar I to carry the values while it is looping in and I will write the variable which is holding all the
objects which is employees pretty simple and then keep your mouse cursor hit enter and use two curly braces hit enter so this is your construct for
4 now what would happen if I'm going to say dollar I Dot and and I'm going to use the property called name
property so dollar I do name okay so name property I'm going to use so what it'll do it'll go to this list and look for the name properties and their values
and is going to print them okay that's how it works now if I'm going to run it okay so you can see Alice John Bob let me clear the
screen run it again you can see Alice John Bob has appeared now let's say I'm going to print multiple things let's say name
age role I want to print one after the other so what you do use a as I said use a dollar you know double code close the double code and inside
this so you can see the do name has changed itself as a string normal string so that time what you do use a dollar symbol in front and close all of them
within a parenthesis okay so I want to print the name then I want to give a
comma and then print the H the same uh structure you have to do
dollar parenthesis and then write dollar I dot h can see the properties
appearing similar way use a comma again dollar symbol parenthesis dollar symbol I do
rooll okay so now what is happening now all the things will be printed for us when the loop is continuing Alice 45 manager John comma know like this it
will get printed if I'm going to run it you can see how the output has come Alice 45 manager John 25 developer Bob 28 test so this is called a list of
custom object let's learn a very powerful feature in power shell called pipeline so what is a
pipeline let me write something so pipeline so what exactly pipeline means
so this means you are going to pass output of one command as input of
another command so this is pipeline so what does exactly this means let's understand so for example I have a command
one so whatever the output of that command comes that will become input for the command
2 and whatever the output of command two it can become input of command three like that it can you can continue as long as you want so what we are understanding
every command creates an output and that output can become an input for this command to and that will process and that it will produce another output and
that output becomes input for the another command the next command the command that is on the right side so what is it let me give you a very simple example to start
with let me write something a string called hello world so this is what this is a text so if if I simply run this what would happen if I'm going to Simply run it so
you get printed hell world so there is output so Hollow world is the output now what if I'm going to pass this one as an
input to the next command for example what I will do I'll use a for each object command L for each object command
L and in this I'll use two curly braces as you know for each you would have to use curly braces to pass this
object to this next object you have to use a very powerful commandlet called dollar underscore and then put a dot
what I'm doing dollar underscore dollar uncore is the one which represents the left side output what it is the dollar uncore
represents the left command output whatever the output of this command is this is carried by this dollcore simple now once I have this output let's say I would like to convert
this to Upper so what would happen this hello world becomes an input here right and it'll go through that entire each
character and is going to convert it to two upper we close the parenthesis okay so this is this is a classic example if I'm going to run this you can see what
happened the hello world is now converted all into uppercase so that means the input of the
first command is passed as the input for the second command the output of this passed as input for the second command
okay now this is one example now let's see few more example to understand this for example you have learned about
this verb and then hyphen this is nomat right this is the syntax and I'm writing process so get hyphen
process if I run this what is the output let me highlight this and simply run on this line so the output is it tells you all
the processor that are currently running in your system currently the processes which are on correct so here notepad is one of them okay so what I will do I
will go through this process through a four four so I'll use uh another Clause called where object so where object is notepad so
instead of seeing all this output I just I'm interested in only one process which is notepad so I'll say where object and
again I'll use curly braces dollar symbol uncore which represents the output of the previous command so the output of the previous command is so
many processes that you see on my screen right here in the output panel so now what I'm doing I'm using Dot and I'm using the property called name so every
process will have a name so for example all that you see on the extreme right Chrome these are all names okay process names so this has a property called do
name so I'm going to use the dot name and then hyphen equals to so this is the operator that we have laed right this is the equals to operator I'll say
equals to and in double code I'm going to say notepad pretty simple look at it so what I'm saying get process get all the processes that are running in my
computer where object name is equals to notepad now if I run this particular line now what would happen it showed me lot
many things only around notepad do you see npm PM CPU 0.05 I ID is 2 3 4 4 0
one you know one instance is open and notepad I got it so I got all the details only specific to notepad now let's say I'm not interested in all
these columns what I'm interested in ID name and right these are the two things for for example I'm interested ID and name
so what I will do again I'll use a pipe symbol and write select object and then I'm going
to add the column names which I am interested in so for example I interested in ID comma name so these are the two things I am interested in now if
I'm going to run this particular line now you can see the ID and the name has appeared so here what is working the pipe symbol that we have used here is
working so you are using a pipe symbol to pass the command okay so this is called pipeline let's take few more example okay for example
I'll we have know let's go through another command hit enter so get service okay if if you use this command lit and run this
command lit what happens it will tell you all the services that are there all the services in your computer are there it is going to show you all the different
services let's say I am just interested the services which are running so again I'll use a we object commandlet and here
I'll use same syntax very simple dollar uncore Dot and here I'll use the property called status running is a status right status equals to and I'm
going to say running okay so what I've written get service where object start whose status is running only only those you show me
now if I'm going to run this particular command lit so you can see the command has run and these are the things which are currently running okay so these are
things which are currently running so this is how you can use the pipeline to pass the output of the previous command to the next command let's see few more
examples okay for example uh let's say I would like to understand how many files are present in a given path in a folder
path okay for example I'll say get uh hyphen child item and then I will mention the path so
path should be in the double quote so for example um let me get a path of any folder so I'm just call copying the
path C drive documents let's say I'll go to Python scripts I'll just copy the copy some path okay and I'm going to paste it
here now let's say I will just keep it until documents okay now what would happen if I run this command get child item of this so how many folders files are there in how many files are there
inside this it should show me so for example if I'm going to run this okay so it tells me these are the different different different files you have url. csb what is asend automation
have url. csb what is asend automation vendor list or something something all the files I have in this particular path now once I get all these files what I'm
interested show me the files which are greater than 1 MB I want to see okay now to uh see all the files right what I
have to use where object uh in the curly braces I'll again use a dollar symbol
underscore dot I would use the length length is greater than 1 MB what I'm writing where object whose length is
greater than 1 MB okay so this is my command which I have typed now what I am going to do I'm going to run this entire command okay okay let me select the entire
command okay and I'm going to run it so if I'm going to run it it told me out of all those file that you saw in that greater than 1 MB is this file what is agentic automation this is the greater
than 1 MB this file was created on so and so time so everything is shown so like this these are the this is very powerful feature called pipeline you can see the output of one command I'm
passing it to the other one and trying to get the output so pipeline is one of the powerful feature in Powershell script the next thing we are going to
learn is if else if else this is a conditional statement and how this conditional statement Works let's see it now for this let me first create a
variable called H we'll take a very simple example and I'm giving some value to it 25 so how do you write the if
command lit for this you have to use if okay and hit enter so automatically you get entire structure for yourself you don't have to really remember
parenthesis curly braces so this is how we can easily do it on uh vs code now once you have it all you do the condition is already highlighted all you
do start typing so I will say dollar age and then hyphen less than equals to 18 if the age is less than equals to 18
then I will highlight this and write hyphen output this is the command lit to print something okay write hyphen output
and here I'm going to say uh within double codes I'm going to say Ur output and then I'm going to say so for
example you don't remember okay is it working or not simply write output iph output and hit enter okay and then within the double quote I'm going to
write you are a minor okay done and then let's go and here in the next line in the seventh
line I'm going to say else if again hit enter then start typing dollar age uh less than equals to 18 here I would say
age is greater than um 18
and uh dollar H uh is less than uh yeah less than 60 or less than equals to 60 okay so I'm saying less than equals to
60 if this is the condition then what I want I want to print this as an output wherein I would say you are an adult you
are and ult simple now come to the next line so here I'll say else else hit enter and here I'm going to again copy
paste my output and I'm going to Simply say you are a senior citizen pretty simple so this is what I
have written now pretty simple all you have to understand if else if else this is how it works based on the requirement you will be using them and if I run the code
uh right what put uh you are an adult there is a problem let me fix it let me put them inside a parenthesis I guess
I crol x v um parenthesis contr
X okay let's see it okay looks okay to me let me run it again let me clear everything and run it again okay so now you can see it is
working you are an adult I just forgot to put the parenthesis so you are an adult that's working now for example if I change the value for example I make it
to 60 uh let's say 61 if I do it and if I'm going to run this it says you are a senior citizen okay if I make it to
60 and run it says you are an ult if I make it to maybe 16 and run it it tells me you are a Min
know so getting it so pretty simple code this is how you would be writing a if else if else conditional statement the next thing we are going to
learn is a switch statement so how this switch statement Works let's see it now what is the switch statement if else
else if all this you have seen now imagine you have got 10 such conditions are you going to write if else if else if else if else if else if the your
coding becomes more complicated when you look at right so there is something called switch statement and how to use that let's see
it for this all you have to do simply type switch hit and and the vs code is going to give you the entire syntax that means you don't have
to literally remember all the syntaxes automatically with the command lit everything is coming for you now here it is asking to write a variable dollar
symbol is variable so let's do one thing let's create a variable first dollar and I would say input
one equals to I'm going to give some color here for example yellow and then here in instead of dollar X I'm
going to replace that with the dollar input one okay done and after that all you have to do
within this two curly braces you have to write all your conditions now what are the conditions I'm going to write so remove this word
call condition and write for example if it is let's say if the color the input
is red that means I would like to write output okay I would like to write output what I will say stop like in a signal
right in the traffic signal so red means stop okay done now as many as conditions I need I can write it so copy I'll hit
enter paste it and I'll change it to Let's say yellow and I'm going to say instead of
stop I'm going to say ction similar way let's copy paste once again and instead of red I'm going to say
green and in the in the output section I'm going to say go stop caution go and anything else there is something called default how to use the this default
apart from these three colors if somebody's providing any other color okay so that time what to do there are a lot of gaps let's remove the
Gap okay now all I will do I'll copy this one paste it here and I will write an output which will
say unknown okay unknown color anything you like you can mention so you can see the same thing if I have to write in
a if else if have to write if and then else if else if else if else so this gives you more a easy way when you have multiple conditions now imagine you have
10 such conditions it is just in one straight line easy to read easy to understand easy to execute so let's do one thing so how it works let's see it
I'm I'm right now given the input called yellow I'm going to run it so what is the output I got
caution okay let's change the color for example I'm going to say red and run it then you can see it said stop red means
stop now if I say green and I'm going to run this it says go if you put any other color
for example I say blue and if I run this you know it's just unknown color so switch Cas is very very powerful when you have multiple conditions the same
thing you can also achieve with if else if but the recommended one for one variable can contain multiple different values and based on the variable value
you would like like to take a decision then this is the more ideal way to go for now the next thing that we should learn
is a do while so do while is a loop that executes a block of code at least
once and then continue executing the block of code until condition is true so what is that entire meaning let's
understand via an example let me create a variable called count and I'm going to give some value let's say I'm going to give a value called zero this is a integer variable I
have created and then I'm going to use a do while loop so the moment you type do
here in your vs code simply select do while and hit on enter now what happens by that is you get the entire structure of the code automatically printed for
you you don't have to really remember now here what I will do in the do right it clearly said it's a loop that executes a block of code at
least once so if you see the do and then there is this is the space where you can write whatever you like to execute let me
write output okay write output and here I'm saying um I I'm trying to print the uh variable so I'll say uh
dollar and then I would like to print the variable dollar count okay so first time when the loop runs it is going to print
zero okay and then what I'm going to do I'm going to increase the value by one so how how you do it in Powers shell you
simply say count and plus plus so what happened the zero will become one then the loop continues it become 2 3 4 like that so here the place where you have to write the
condition okay so how do you write the condition so here I would say dollar count is less
than 5 so until it is less than five you continue the loop okay until it's less than five you continue the loop now the moment I am
going to run this what would happen you can see the printing has happened 0 1 2 3
4 now what would happen if I if I make it to less than equals to so it will
also print 1 2 3 4 5 0 1 2 3 4 5 so basically this is how a do while loop works and at least even the condition is not me let's say I'm going to say less
than uh less than zero for example I put the condition so what will happen because of this you see the output only thing it printed is zero at least the
first line will get printed and the rest of the things did not print because the condition is less than equals to 0 so 0 is already zero right so so this is why the condition
failed here because the condition failed the loop did not continue the moment I make it to one and run it you can see it has
printed less than one I said okay that that's why it has printed zero if I say less than equals to
1 and run this code see it has printed 0 and one so this is how the do
while you know statement is used in Powershell so this is the last section of the Powershell beginners video that
is try catch finally to understand try catch let me show you few things let me open a notepad and here let me write some text
for example hello [Music] Rakesh let's save it on my desktop let's go to the desktop and you
can see the file exist here I'm going to right click on the file and try to get the path or the location of the
file go back to my code and here I I'm going to do something I'm going to use the get
content so I would like to read the entire data that is there inside the text file and then I'm going to say path and I'm going to copy paste the path so
is going to read that particular file from this okay now if I run this what happened the content that is
there inside the file is getting retrieved for example let me go back to that file once again open it up and let's write something else let's say
I'll say hello W and I'm going to save the file go back to my code and if I run the
code now what is happening hello rakes hello what this are the two whatever content is there inside the file line by line it is reading them and printing in in the powerwell
script this is fine now what exactly I would like to teach you for example in the second line I'm going to Simply copy
paste this until here and I'm going to write nonexisting
file.txt so this file doesn't exist actually and if I run this what happened you see an error which
appears you see there is an error which appears if I write a line below this for example I'm saying
hello okay now if I run the code now just check the behavior of Powershell so what is happening the hello still getting printed hello everything got
executed see hello rakish hello world and in the fourth line line number four you got an error because this file because it does not exist and then
Powershell continued into the Fifth Line as well and printed Hollow for me so as a default behavior of Powershell
what it does it does not stop if error comes it still continue to execute the rest of the code blocks or the code line of
codes try catch so what is TR catch so try catch means for example I would like to try to read the content of this file which currently doesn't exist for us to
understand when I reading the file if the file does not exist there will be an error so what I will do using the catch
command let or using the catch command I'm going to catch what happened did the file been deleted is the file does not exist or is the file been used by
somebody else you can think of many conditions now finally finally means let's say you got the file or in case the file is not there then you would like to read from
any secondary file for example this is my secondary file where I would like to read it from if it'll try for the first file does not work then it'll write what error happened then it'll try to open
the content of the other file and finally you would like to close the operation so that is the meaning try catch finally pretty simple right now what I'm going to do I'm going to keep
all of this as it is okay and go up and start writing the try catch blog pretty simple guys so simply say try catch
finally you'll get in vs code hit enter now it look little up you know little U complicated because of the curly braces everywhere but this is very simple okay
absolutely simple all you have to do within this curly braces you are writing your code what you want to execute within the catch you would like to see what you would like to catch uh and and what is the alternative action would you
like to take if that error happens and finally you finally want to close the operation now let's write the code in the try what I want to try I would like to try
this okay and very important you need to know the nature of powers shell Powers shell does not terminate does not
terminate so for this you have to write a command l called error action and then stop so if the error
have you know if if this command could not execute don't move on to the next command let's say below this I have written something else okay so for
example below this I will write an output write I I'll show you how it works first of all okay so write output um and I'll
say have file exists okay so generally if I don't write this okay if I don't I'll remove
this and if I run the code so what happened see um I'll just put a few has
symbols okay and then let's let's minimize this so now what I going to do let me not delete all this green
ones okay so the code is pretty simple now I have not written anything it's all blank now if I'm going to run the code just check what is happening see above
file exist it is writing the next command also that means the line number five is getting executed wherein I don't want to execute when there is error it should move to the catch block now for
that what you do all you have to write a simple code called error action and then you say stop when error comes then stop
for example what I uh okay so for example if I say error action
stop and then if I execute this code now this line will never be executed look at it see last time it has executed above file
exist now here what happened it didn't execute also see Power script uh script that's all it did not even execute it stopped so that is the difference you
have to write error action stop this is clear now once it stops what you would like to do with that you would like to catch the error and write what error
happened clear so what I will do I'll use a right output and here I would say in double code error colon and within double code when
you write remember if you have to use some variable or an object what error appeared you have to use dollar symbol
parenthesis dollar symbol underscore dot underscore do we have learned this will carry that object value and here I would
say error right um uh error dot error details. message I will SA okay err
details. message I will SA okay err details. message now what happens by
details. message now what happens by that let's run and see if I run it see uh it said
error uh and then uh there is a problem okay so what I'm going to do instead of writing error details let me
write uh exception. message exception do message okay now if I'm going to run the code you can see the exception messages got typed Let Me Clear there are a lot
of things and we are really not able to understand let's run it so what is happening here error cannot find path so
it is catching that error through this line do you see it says because it does not exist so it caught the error now if it doesn't exist what would you like to
do next so for example if the above file doesn't exist I would like to open this file I'll go up and I'm going to write it
here pretty simple I'm going to write it here hall. text and if I run it okay if
here hall. text and if I run it okay if I run it so what happened you see um I know if you can clearly see it or not let me clear everything and run it again
so what happened let's understand now the tri block ran the fourth you know the file doesn't exist so it came to cat section and then it should write that error what happened so here it has
written error cannot find you know error the way I have typed it has typed it cannot find P this and this and then in the get content right it tried to read
the content of another file alternate file so this is the help that TR catch provides you if this something doesn't work you want to do something else you want to catch the error what happened so everything can be done in a proper way
so hello rakes hello world that content of the text file is written and then closed now what is the use of the finally section so sometimes finally section is like closing operations for example you
want to close the file you would like to do something you want to close the browser you want to anything you would like to do you can write in finally now for our purpose what we will do we'll simply write a
message and I'm going to say file operation closed file operations closed pretty simple
and if I'm going to run this you see Hello World hello Rakesh um you know error it printed the error error hello rakish hello world and file operation closed so this is how the
try catch block Works in Powershell script and thank you very much to watch the video till now you have lot of patience please do comment in the
comment section of the video that you have completed a video say I have completed the video I'll be happy to see the names of the people who have completed the video from the start till
the end thank you guys for watching let's meet some in in some some of our interesting videos that going to come on my YouTube channel thank you
Loading video analysis...