 to start with the introduction of PowerShell, then we will see some basics. I am not going to get into the very basics, we will start with the basics which are required for you to start using PowerShell. In this, we will find how to find commands in PowerShell, how to make use of the built-in help system, when you have to use those commands, how to manipulate the output of the commands after you run command you will get some output and if you know how to manipulate the output you can make better use of that output. Then we will talk about the very important feature of PowerShell that makes it stand out from other shells like in Unix you have shell, bash shell, so how PowerShell is different than other shells is its pipeline feature which makes it very powerful. Then after getting introduced to the pipeline we will directly jump into writing your own PowerShell scripts and how to publish the scripts that you have written so that others can utilize. If you are working in a team whatever you develop unless you make it available for others it is limited only for you and it is not going to make a big impact for your organization if you are not making your scripts available for others. So while getting introduced to PowerShell before I start about PowerShell let me just talk two minutes about why PowerShell. Prior to PowerShell there were other shells like bash, there were other scripting languages like VB script and in windows the shell was command prompt. When you run a command in command prompt that produces some output which is a plain text. For example when I run dir command here what you see on screen is a plain bunch of text. Now if I ask you I just want the last column I do not want to see this timestamp this dir and all I just want to see the last column and after last column I want to see the length of the file. This last column is the name of the files and folders which are available in this folder and this is the length column. If I ask you to manipulate this according to your needs in this command prompt it is very difficult you cannot easily achieve that but if you try to do the same thing in PowerShell when I give dir command here it works a bit different. So let me go to the same folder CD so that you can relate it. Now in PowerShell if I want to rearrange the columns or choose what I want from this output I can easily do that by giving dir and pipe it to select name comma length. This flexibility was not available in traditional command prompt even in Linux if you want to manipulate the output it is not so easy as compared to PowerShell. PowerShell makes it possible because in PowerShell every command that you run produces an object it is not a plain text it is an object. We will talk more about objects in the third topic when we will discuss about how to manipulate the output of the commands apart from that another reason why PowerShell is becoming more popular is you do not need to install it anymore you do not need to install it separately it comes with the operating system starting with Windows 7 from the client side and from the server side Windows Server 2008 PowerShell has become part of the operating system installation you do not need to separately install it. That is the reason PowerShell is now a choice of automation rather than installing some other tools and then automating using those tools apart from PowerShell Python is also a very good tool for automating your stuff but Python unit to install on Windows it is not by default available for Windows operating system this is already there and when we talk about the latest versions of Windows operating system especially server side we have Windows Server 2016 Nano Edition where without PowerShell you cannot do anything on that server it is like a headless server when you log into the server you will not find a command prompt you will not find anything you will just find one screen where you can do some basic configuration like IP address DNS and all those things but if you want to manage Windows Server 2016 Nano Edition you have to remotely connect to that server using PowerShell and if you do not know PowerShell you cannot manage that server and the reason behind making it this way is to keep the operating system footprint lesser the disc footprint of Windows Server 2016 Nano Edition is just 400 MB now in this cloud of this era of cloud disk space matters processing speed matters and keeping your component lightweight matters a lot because in cloud when you go for a VM if you want to purchase a virtual machine then the cost depends on the disk size the processor and also how much network bandwidth your VM is consuming based on that the pricing is done so to reduce the cost Microsoft has now started giving lightweight operating systems which can be spin very fast if you purchase a Windows Server 2016 Nano Edition server in cloud Azure cloud it will spin up very fast within few seconds that machine will be up and running so looking into all these things PowerShell has now become mandatory for everyone who is looking at automation and managing Windows platform so as I said PowerShell comes by default with the operating system when you click on start button type PowerShell you can start PowerShell so in PowerShell by default there will be two flavors one is PowerShell ISC and console so when I click on start and type PowerShell you can see there are four shortcuts for PowerShell out of which anything that ends with x86 that is the 32-bit version of PowerShell others are the 64-bit versions and if you see there are mainly two flavors one is console which is just Windows PowerShell and ISC now if you start console this looks very similar to our traditional command prompt the only difference that you will see by looking at it is the background color is blue rather than black but this is not a command prompt this is PowerShell console here you will be able to run all the commands which you were able to run in the traditional command prompt apart from that you can run all the PowerShell commands as well now here there are certain features like tab completion suppose I want to get list of all the services which are running on this computer I can type get hyphen s e r and when I hit tab it will autocomplete that command with the command that begins with these characters it will give me list of all the services available on this computer if I type get hyphen p ro tab it will autocomplete it to get process this tab completion works for command names as well as command parameters parameter is the option that you want to use with the command suppose you want only those processes where the name begins with say any alphabet hyphen n tab will autocomplete it to name a star all the processes where the name begins with a so this is called tab completion feature this is available in console and isc both so I also have another window which is running PowerShell isc this is isc if you look at the visual differences isc is mainly used for developing scripts while running commands in console you do not have an editor available but in isc you have the script editor available here you can write your programs or scripts this editor is available now the question is why we have two flavors is there are certain commands we can call them the legacy commands those are also called interactive console applications wherein when you run that command for example nslookup the moment I give nslookup you can see the command prompt has changed just showing this symbol instead of showing the complete path so here if I type google.com currently I am not connected to internet so it will not work but if you are connected to internet you can try this it will show you the IP address of google.com this is name server lookup this gives details about the DNS entry for this name or if you give an IP address it will give you the name like google.com name server lookup this is called an interactive console application because here when you give some other command like dir this will not be interpreted as a command it will consider this as name of some website or some domain or some computer and it will try to resolve it this is called interactive console application because here the meaning of command will change because you are in a different context you are in a name server context here if you want to go back to the normal command prompt you can exit from here now you are back to normal command prompt here if you try such command like nslookup it will fail it gives interactive console applications are not supported isc does not support these kind of commands where the command prompt changes the context changes to some other application if you want list of all such commands which are not available you can copy this variable it right here so all these commands are not supported in isc but these are the commands which are still supported in console that is why we have two different flavors of power shell still available but on windows core you do not have isc you just have console now what is different in isc as compared to console first thing is the gui the isc stands for integrated scripting environment wherein you can develop scripts apart from running commands another good feature which is available in isc but it is not available in console is intelligence when you run a command like get hyphen the moment you give hyphen it will show you list of all the commands that begins with get hyphen and as you keep on typing it will eliminate other results and will keep only those results which match your search criteria for example if I type ser as you can see it is showing only those commands where the first part is get hyphen and after hyphen second part will have these three characters ser like here it has in server you have ser if you look at any example here which is still in the suggestion list it has ser in the second part after hyphen and here if you give tab it will autocomplete that command so this tab completion which was available in console is still available in isc results will be same and here apart from the command name it also works for the parameters like we have these parameters to get the services by their name if you want services from a remote computer you can use hyphen computer name and provide the name of the computer from which you want to fetch the list of services so I will give it pogo this is my local computer name it will try to get the list of services from this computer and here sorry computer name and which service I want I want all the services where name begins with a pp so it is showing me the list of all the services where the name begins with a pp from computer it pogo so we have seen tab completion and intelligence now let us take a look at why running power shell as administrator is important and what happens if you do not run power shell as administrator what do I mean by running as administrator when you have to start power shell suppose I am starting power shell from the start menu I have to do a right click here and click on run as administrator what happens if I do this and if I do not do this what is the difference so the situation where you start just by clicking on windows power shell without doing a right click and run as administrator you will get a window something like this where the title bar says windows power shell isc if you have started isc without doing a right click and run as administrator it will just show you windows power shell isc in the title bar and when you do a right click on the shortcut and run as administrator it will show you are running as administrator why does this matter it is not just the only reason we are running as administrator to see administrator in the title bar anything that can make changes to your computer state will work only when you are running as administrator for example if you want to restart a service or if you want to kill a process you can do that only when you are running power shell as an administrator let us try this get hyphen service let us try to restart this service pipe it to restart service ok since this is an antivirus service it is not allowing me let us try another service bits ok this service when I try to restart bits it did not give me any error because I am running as administrator I am able to run this command successfully from power shell if I try that from the window where my title bar does not say administrator get hyphen service hyphen name bits restart service so here since you are not running as administrator it will not allow you to restart the service forget about restarting a service you cannot do anything that can change state of your computer sorry it will run under your current logged in users context yes I will show you that was a good question dollar env or there is another command called who am I so it is currently running under this user and if I this is from the normal where I am not running as administrator and here if I try who am I same user but different privileges just because it will elevate your privileges when you right click and run as administrator only then you will get those extra privileges the reason for this is to keep yourself from accidentally making changes to your computer especially in production environments in when you are working in big organizations on a production server if you want to play safe if you want to keep yourself from doing some accidental changes to the server it is always better to run as normal window without doing a right click and run as administrator any other questions ok now the command for checking the version and what is the recommended version let us discuss about that get hyphen host will give you the current version of power shell that you are running we are running 5.1 this is the most stable and most widely used version of power shell currently in all organizations in production environment though power shell version 6 is available there are certain features which are removed because power shell version 6 Microsoft has developed with an intention that they will allow non-windows platforms as well so this is the most stable and widely used version so I recommend if you are using power shell use this version and power shell is freely available you can search for windows management framework or you can directly search for in Google you can search for power shell version 5.1 download download it only from Microsoft not from anywhere else so it will come as windows management framework version 5.1 when you install that it is like a kb some patch when you install it after restarting you will find that you have the latest version of power shell ok before we move to the next topic any questions from the introduction part ok we discussed about architecture power shell has two different architectures the same exe which is available on your machines it will have two versions x86 which is the 32 bit version and 64 bit version mostly if you are developing scripts for newer environments it is better to stick to 64 bit version 32 bit version you should use when you have a mix environment where I do not think nowadays any organization will still have 32 bit computers in their environment very soon even this 32 bit version will be gone the reason we should care about 32 bit or 64 bit is only situations where you are accessing windows registry keys in windows for 32 bit version and for 64 bit version the path of certain registry keys is different so if you are writing scripts which are you think that it can hit a 32 bit version machine as well it is better to keep that architecture check in your script before you deploy it in production because if you are running a script which is developed on a 64 bit version of power shell and you did not check the compatibility with 32 bit it might not be able to run successfully on 32 bit version so for example here if you want to find out what is the architecture there is a dot net class from which you can find the architecture int ptr colon colon this will give you the architecture in bytes but we do not talk about architecture in bytes we talk in bits so how do you convert this byte into bit multiplied by 8 because 8 bits is equal to 1 byte right so this is 64 bit the version that you are running currently it is 64 bit version of power shell so in your script in your program if you want to put a logic to detect where the computer against which you are running the script is 64 bit or 32 bit you can apply this logic you can put this class and look at the result of this now just to show what will be the result of this if you are running 32 bit version int so currently I am running 32 bit version of power shell so the result here it shows it is not showing the result of operating system it is not showing the result of your processor architecture it is showing the result of what version of power shell exe you are running you are running the 32 bit version of power shell exe so throughout this course we will stick to isc because it has more features than console and we are not going to use any of those interactive console application commands which are not supported in isc another command for getting the version is dollar ps version table this is a built in variable which is defined inside power shell that also gives you slightly different results here it shows you the addition so it is the desktop edition of power shell on server you will find a different one when you log into a core server you will find a different one so there are different editions of power shell this is how you can check the edition and it also shows what are the backward compatible versions supported in this version so this version of power shell supports version one two three four and five because we are five currently we are at five now before we begin this topic let me tell you in order to start using power shell efficiently there are four things if you learn those four things if you understand those four things you can start using power shell efficiently you can fully utilize the power of power shell those four commands are let me type those four commands here first is get command get help get member and fourth one is not a command it is a concept pipeline if you understand these four things you are done with power shell at least the basic part not the scripting part but even for writing scripts the understanding of these things is very important so the topics initial topics that we are going to cover like finding commands will take care of this first command get command get help will be covered as part of using help system topic and this topic manipulating command output will take care of get member that third command and if you understand these three topics you are good to start so let's begin with finding commands so in power shell Microsoft has introduced a new word called command let CMD let before power shell this word command let did not exist this word was introduced along with power shell in the market there was no word called command let before power shell it is related only to power shell so Microsoft has done this intentionally because when you search on Google or any search engine using command let keyword you can try that any result that you will get is related to power shell only for example if you want to find a command to get list of all the services and if you try with the keywords commands command for getting list of services you will get all different results from all different platforms it will show you windows commands Linux commands even Mac commands but if you are searching only related to power shell then use command let for getting services it will show you results only related to power shell because this keyword command let did not exist okay so in order to find commands in power shell see whenever you are dealing with a shell any shell that you talk about will not have any GUI interface it will not have any icons yeah there will not be any icons where you can just look and click you cannot navigate to the features so easily unless you know what commands are available when I was new to computers when I was in school when we first got our first computer lecture and we started using computer they have shown us the first command as dir that shows list of all the files and folders then they every day they used to introduce a new command and whole day we have to run only one command like date time dir copycon so these are old commands when there was no GUI that time so we used to guess what other commands could be there because our lecturer is so lazy he is teaching only one command every day so we were curious to find other commands and we used to try stupid things like if we want to find the result of a equation we used to directly type that equation there hit enter it shows bad command or filename so that problem was there because there is no GUI you cannot easily find stuff which is available unless you know what commands are available same goes with power shell unless you know what commands are available you cannot do anything what will you try so in power shell that problem is solved very nicely because power shell follows very specific naming convention for the commands that makes it easy for you to guess commands the thing that we were doing those days guessing commands so it is very easy here in power shell because of the naming convention that power shell follows and what is the naming convention that power shell follows every command in power shell will be verb hyphen noun that is the naming convention it begins with a verb then a hyphen or dash then it ends with a noun and that noun is always going to be a singular noun there will not be any command like get services there will always be a command like get service get process it is always singular so first problem is solved like how to guess commands when you know that the command name is always going to be a verb hyphen noun now second thing that you need to think about is what could be the verb and if you got the verb give a hyphen it will suggest you in the intelligences what this command can end with it will suggest you the noun part right so suppose I want all the commands which are available in power shell for exporting something out of power shell so I can give export hyphen it shows everything which is there for you to export all the commands related to export similarly if you give import give import hyphen anything that is going to get something from the computer not changing like if you are reading the configuration if you want to get list of services if you want to get list of processes if you want to get some objects it will always begin with get hyphen just give get hyphen half the job is done now if you are looking for certain kind of objects just type object so there is only one command get WMI object and the moment you your pointer goes to that it also suggest you what could be the syntax of that command in this tooltip okay we will see what is get WMI object very soon before we go there now the question that comes to my mind after I have this knowledge is how do I find list of all the verbs and I am sure one of you can easily give me that command which can give me list of all the verbs what could be the command for getting all the verbs sorry getting verbs get hyphen verb now this solves half the problem of finding commands because it will give me list of all the verbs which are available in power shell now see these are the verbs available ignore the second column look at the first column verb so any command in power shell will begin with one of these verbs it will either begin with add clear close copy enter exit find now it is easy for me to guess the commands now because either I have to guess the verb and if I want to confirm whether the verb which I guessed is available or not I can look at get hyphen verb command results and find out if that verb is available or not now for example I want a command that is for protecting something give protect hyphen so what that command does that is secondary but at least I am able to find a command that begins with protect hyphen and the second part gives me details about what it can do similarly if I do test hyphen these are the all things which I can test test certificate test app locker policy test connection test the AC configuration now if we go back to our list of commands which I mentioned are important first command which I said was get command get hyphen command get command allows me to search for the commands if I am not sure what is the exact command for example whenever you do something in your operating system there is something called as event log which will keep track of what things are going on maybe something goes wrong with the server tomorrow when company investigates who did that they will look at the event logs that is the first source of information when they have to do cyber forensics when they have to investigate some case suppose somebody has screwed up a server and they want to find out who did that they will first look at the event logs how do you get the event logs get hyphen either you can guess and you will guess it correct but suppose you do not know what that command can be you can use get hyphen command and give name star event log star what does this star means that a strict that means the command might begin with anything first a strict means it might begin with anything second a strict means it can end with anything but somewhere in the middle I am looking for this keyword event log it will show me all the commands related to event log clear event log get event log limit new remove show write these are the things which I can do from power shell related to event logs so when you use hyphen name it will search in the verb part and noun part it will completely search for that keyword in the command name as I said command name has two parts first is verb and noun but if I am not looking for either verb or noun I can use hyphen name and it will search throughout the command name not only the first part or second part but if you want to narrow down your search you can also use get hyphen command when you are sure what is the verb like if you are looking for command with the verb get but you are not sure about the noun part here you can use star event star now what does this mean the first part that is the get part or sorry the verb part it will look for this exact keyword get it will show me all the commands where the verb contains get and it will look for this keyword event in the noun part of the command now if I hit enter see here it is showing me all the commands where the noun part contains this keyword event so there are so many commands which also includes get event and get event log so this is how you can search for commands any questions then comes running external commands before we go there let me show you get hyphen command if I do not use any parameters here just hit enter it will show me all the commands which are available on my computer if you want to find how many commands are there you can pipe it to major 1861 commands but if you try this come the same command on your computer you will find there are more or less commands on your computer it could be more or less than this that depends on what operating system components are available on your computer for example on my computer I have installed an optional feature that is hyper v if your computer does not have hyper v all the commands related to hyper v will be subtracted from this number 1861 instead of 1860 it will show you less number of commands if you do not have hyper v so depending on what operating system features are available it will show more or less number of commands are we all on the same page any questions does this make sense it is yeah it is giving the number of items in the result but there are certain cases where when we look at the command like dir here there is a property called length right so if you want to find the average length you have to specify in measure the property name and what operation you want like if you want the average use hyphen average so when you give dir pipe it to measure property length here you can give some average maximum minimum I have selected all the options it will show me the number of items in the count what is the average file size what is the total size of all the files minimum and maximum yes yes yes yes you need to be aware of the property beforehand before you use it for example when I run a command called get process and if I am looking at what is the maximum CPU time and what is the minimum CPU time used by any of the processes so I can pipe it to measure property CPU maximum and minimum and also we can use average so maximum CPU time used by some process is 183 seconds and average is 8.1 so that depends on the property that you select okay so I was at running external commands when I have shown you get command it shows me list of all the commands wherever you see this keyword command lit that is power shell command as I said because this command lit keyword did not exist earlier all the commands which has this keyword command lit are power shell commands apart from that there are functions which are part of some modules we will come to that modules part later and there are certain applications like there might be aliases and also in few cases you will find certain applications which are available in the list those applications are the exe files or anything which is not part of power shell for example if you want to start notepad you can directly type here notepad if you want to start calculator it will start calculator so these are external commands power shell allows you to run all the legacy commands external commands plus power shell commands what are other legacy commands like ping this command test the connection with the remote computer so currently I am pinging my local computer I do not have internet access so when you run this command ping this is not a power shell command the equivalent command in power shell for ping is test type in connection and you can specify how many times you want to test with count I will just keep it one or two now the difference between this ping command and test connection command is ping is a legacy windows command which is an external command for power shell but test connection is a command which comes with power shell it is a command let that is the primary difference and secondary difference we are all aware first command produces a bunch of text which you cannot easily manipulate for example if I ask you I want only the IP address in the result I do not want reply from and whatever comes after that if I want to filter out unwanted things if I want to pick only the IP address you cannot do that with ping command without applying a very complex text parsing logic here with test connection you can easily do that by piping it to select IPv4 address because the second command test connection produces an object it is not a plain text we will talk more about objects in next topic after discussing the help system any questions so far okay in that case you need to know the exact executable path for example if Skype is installed in the program files then backslash Skype and you need to know the path of that exe file you have to give the complete path then only you can run it any other questions okay let us move to the next topic using the help system now we have achieved the first step of finding the command so we are going with this approach because PowerShell does not have GUI the first step is to find the command the second step is to understand how to use that command that you can understand by using the help system now PowerShell has very good built-in help when you are using PowerShell you do not need to use any search engines whenever you are stuck for any reason in PowerShell all the information that you need is already available in PowerShell with the built-in help system so you can type get-help and give the name of the command for which you want to understand how to use it for example get-service so by default it will show you the basic version of help wherein it will show you the command name the purpose which goes under synopsis and if you look at the syntax for this particular command it is showing three different syntaxes and these three different syntaxes are called parameter sets that means these are the parameters which you can use together in a combination anything which is not part of this combination you cannot mix and match for example in this case for getting the list of service you can use display name along with computer name for example if you want to fetch the services from a remote computer you can give the computer name and give the display name of the service that you are looking for but you cannot use display name and name parameter together do you see name parameter and display name parameter in any of these three sets coming together in the same set it is not you can either use display name or use name or you can use input object so each of the syntax has something unique which is not available in other combinations this is called parameter sets there are three different ways you can use this command but you cannot mix and match these three different ways for example if I want get service to give me the service name as bits it will give me the details about the bits service if I use display name hyphen display name background intelligent transfer service in this case I have to give the display name of that service same results but if I use display name and now if I try to use name first thing or first clue that you will get is name parameter is not shown in intelligence anymore but if I forcefully use it read the error parameter set cannot be resolved using specified name parameters power shell is confused now which parameter set you are trying to use because these two parameters differ belong to two different parameter sets none of those combinations has these two things together but if you go for display name along with computer name it works if you go for name along with computer name that also works because in those three sets you can find name and computer name are coming together in at least one set same goes with display name in one of the sets you have display name and computer name together but there is none of the sets where display name and name parameter comes together is that clear any questions ok let us try to understand the syntax now different symbols which are used in the syntax now when you look at the syntax you will find that there are lot of symbols involved for representing how to use this command anything that goes in square bracket is optional that means computer name is optional what what does that mean optional means for running this command it is not mandatory to give hyphen computer name and followed by the name of the computer even without giving computer name I can still successfully run this command this angular bracket means for computer name what type of input is required you can provide a string as input for the computer name string means text and this empty square bracket shows that you can provide multiple computer names here how do you provide multiple computer names for example if you want the details of this bit service from two different computers give a comma and give the next computer name now there are two results one from each computer but in this case both computers are same I do not have an environment where there are multiple computers I can connect to and show you so wherever you see empty square bracket that means you can provide comma separated multiple values and it will show you the results for all the values which are specified and if one of these computers is not accessible for that computer you will find error for example here if I give computer x y z okay here errors are suppressed using there is an option I will mention later using which you can suppress the errors so if you look at the output it is showing output only for two computers because one of the computers is not accessible now let's talk about other symbols as I mentioned anything that goes inside angular bracket talks about the data type what type of input is required for this parameter square bracket indicates that you can pass multiple values of that type so if we look at another example get hyphen wmi object so here you will find certain more symbols like curly brackets so here for impersonation you have this curly brackets and inside curly brackets there are this pipe symbol separated values that means for impersonation you can provide only one of these values impersonation can be either default or anonymous or identity impersonate or delicate we can try that get hyphen wmi object I am just randomly picking any class we will talk more about this later now if I select impersonation the moment I give space it will show me what are the allowed values for impersonation it could be either anonymous default delegate identity or impersonate sorry identify or impersonate okay what that impersonation does that is a different topic but here we are trying to understand the meaning of this curly bracket whenever you see there are certain values given inside curly brackets those values are the list of valid values for this parameter if you try any other value for impersonation it will give you error okay anything which is not in square brackets that is mandatory it is mandatory to give value for the class name when you are running get wmi object command if you do not provide the name of the class let us first try with class name win32 underscore bios it shows me the bios details if I do not use this class name directly hit enter it will ask me for the name of the class it will ask me to supply the name because that is mandatory without name of the class this command cannot complete so here when I provide then only this command will give results did we miss any symbol I think we have covered this hyphen indicates that this is a parameter anything that begins with a hyphen is a parameter and there are certain parameters which does not follow any values like as job here this parameter if you look at it it is not followed by any data type like properties followed by string class name is followed by string throttle limit is followed by integer but this as job is not followed by anything these are called switch parameters like switch on or off whether you want to run this command as a job or you want to run it normally presence or absence of that parameter makes the difference it is like Boolean value true or false when it is present that means true when it is not that means false any questions ok next is what other parameters are available while looking at the help like full show window online and example these are the parameters currently when you are running get help followed by the command name it is giving the basic version of help it is not giving the full version when you use hyphen full hit enter this time it will show you lot of other things which were not previously shown like most important thing is this was already there when you ran it without full it was showing names synopsis syntax and description but now it is showing details about every single parameter which is available with this command what is the purpose of that parameter why that parameter is used what type of value it requires whether it is a required parameter now we have seen class was a mandatory parameter right so when you look at the required yes it is a required parameter it is mandatory to provide the value for this this position talks about whether this parameter can pick the value in the command line from a certain position what does that mean when I want to run this command I do not need to give hyphen class always and then give the name of the class I can directly give get WMI object and name of the class without specifying hyphen class let me show you that example so for class when I run like this or since class is a positional parameter and position zero means first position when I do not specify class it will still work because whatever value you supply at position number one that is position zero here it will be considered as name of the class or it will be considered as the value for the class parameter okay when we will talk about pipeline we will come back to this accept pipeline input this is very important when we are understanding the pipeline to check the value of whether this accepts the pipeline input or not we are talking about the parameters whether this parameter will accept the input from the pipeline or not so we will check that when we will talk about the pipeline then comes accept wildcard characters there are certain commands there are very few commands wherein the parameter can accept wildcard characters like asterisk like these are wildcard characters so it this thing tells whether that parameter can accept those wildcard characters or not then parameters will be followed by what type of input can be supplied to this command when we talk about input we are talking about the input coming from the pipeline again when we will talk about pipeline we will talk about input and output what type of object is given as output when you run this command then it is followed by certain examples of how this command can be used and this part is very useful when you are new to power shell looking at these examples you can learn a lot it will show you how this command can be used with the explanation what this particular example does so you can look at these examples and the examples are given with the syntax how to run that command and what kind of output it will generate with the explanation and these are related links like where you can find the online version of this help topic and certain other commands which are related to this if you just want to see the examples you can use hyphen examples with get help use examples this time it will show you only the examples apart from name and synopsis it will show you only the examples it will not show you the syntax or parameter details but my preferred way of looking at help is use show window this parameter is very helpful because it will open a pop-up window on screen which will sit on top of your power shell window even when you switch back to power shell you can still refer to this it will always be on top of your original power shell window this makes it very handy while it allows you to select the sections of the help that you want suppose I want to see only the syntax I can uncheck everything else it will show me only the syntax part sorry yes now if I close this if I try this with get service it will remember what was your last choice now if you change something here again it will remember whatever is your current selection global now you can choose what you want and this is a very handy way of looking at the help while referring to the help you can type the commands you can still use power shell while looking at this window another way you can look at help is online if I hit enter it will not work for me you can try that for your own computers because I am not connected to internet it will take you to the online version of this help topic that is having the latest information okay so that is called commands add on window by default when power shell starts you might have seen this window by default once you close it it will remain closed next time when you start power shell but if you don't close it it will always open whenever you start power shell this is not help window this is when you select a command it will show you how that command can be used for example I can search for a command like get-service the moment I select it will give me a form a GUI form when I fill this form click on run it will run that command for example I want to run whatever you see here without a asterisk symbol is optional but when you select display name this shows this asterisk means display name is mandatory but this is the default here if I give and name of the service either I can click run it will run the command or I can click insert so whatever is there on the command line it will insert this command at the command line now I have removed the previous thing when I click on insert it will automatically generate the command and paste it there or I can copy so that I can paste this manually here I can do a right click and paste okay about about underscore topics so power shell as I said when you have power shell help available you don't need to refer to any search engine for anything related to power shell not only commands if you want to learn power shell certain concepts like if you want to learn about how do loop works or how for each loop works in power shell you have to search for about underscore topics for example when I run get-help about underscore star it will show me all topics that begins with about underscore if it does not work on your computer your help need to be updated with the latest data from Microsoft so for updating your help if you are connected to internet you can run command update-help and give parameter-force force will forcefully update even if it was updated recently now these are all about underscore topics suppose you want to learn about while loop you can use help or get help help is an alias for get help command help about underscore while and as I said I prefer show window so that I can refer to it while running the commands so it will give you complete details how this while loop can be used it will show you certain examples the way you can use while loop so it has information about all the concepts which are available in power shell in these about underscore topics as a beginner again this is very useful for you to learn new topics by yourself once you have updated your help you don't need to have internet connection for learning anything related to power shell everything is available inside okay that completes how to make use of help system this is the third important thing that we are going to learn so first command was get command second command was get help third command is get member let us see what is get member all about but before we move to what is get member let's talk about objects what are objects and power shell as I said whenever you run a command it will produce an object as output a certain type of object when we look at objects of real world then if we relate it back to the objects in computing it will be easy for us to understand so what is an object in real world anything that you can see around you is an object for example if you have a smartphone smartphone is an object now objects are categorized in real world as well as in computing in terms of computers or programming languages we also deal with objects similar to the way we deal with objects in real world now if we talk about a smartphone smartphone is a subcategory what is the parent object type a phone a phone is a parent object type under phone you have smartphone then you have landline phones you have ip phones there are different types of phones which comes under the phone object type but when we talk about a smartphone there are certain features which are common across all smartphones that makes it a smartphone your normal desk phone will not have built-in RAM or it will not have the screen size it will not have the it might have CPU but not the CPU that we talk about it will be just a chip which is controlling its circuit board but when we talk about smartphone it is similar to the processor that we have in our computer it will have a battery not every desk phone will have a battery when we talk about smartphones we will talk about what is the screen size of your smartphone what is the type whether it is 4g 3g 5g what is the battery capacity these are all different properties available for a smartphone and it's not necessary that every smartphone will have same values for these properties every smartphone will have a different screen size the value of the screen size will be different some phones will have 4 inch 5 inch 5.5 inch but screen size property is common across all smartphones if we take some other example like vehicles vehicle is a parent object type under vehicle we have two-wheelers four wheelers or heavy vehicles like trucks under four wheelers we can further have subcategories like luxury cars or hatchback cars sedans again when we talk about a particular car there will be properties like fuel type or engine capacity these are all properties even color is a property color of the car so in computers we have similar type of objects we have service process or WMI object file system object these are all different types of objects which are available in computers so in computers at the root we have a generic object under generic object you have power shell objects under power shell objects you have all different objects which are available under power shell like service object process object WMI object file system object now we have talked about object as a class now when you talk about an instance for example when you talk about your own smartphone now we are not talking about a type of phone we are talking about a particular phone that particular phone will have specific values similarly when we talk about service as an object there might be certain properties which are common across all different services but when we talk about a particular service like bit service then we are very specific we are talking about an instance of an object that object will have specific values the name property will be having the value of BITS display name property will be having the value of background into intelligent transfer service the status will be either running or stopped so these are the properties of an object then comes methods now what are methods when you are taking some action with that object that is a method what actions can be performed with the object like from a smartphone you can place a voice call placing a voice call is a method now for placing a voice call does that require any input yeah for placing a call what is the input you need a number to which you want to call without a number where will you place the call similarly in computers when you have to invoke a method there may or may not be some input required similar to in phone when you have to place a call giving the person number to whom you are calling is mandatory but when you have to reboot your phone do you need to give any input no input is required you can simply reboot a reboot you can call that reboot method it will restart your phone similarly for service if you want to restart a service do you need any input no but there are certain services which can run some special commands for running the command you need to provide the command number some integer so when you are calling that method certain methods will require certain input but there are certain methods which does not require any input if you want to change the startup type of the service services can have different types of startups like some services will automatically start some services will be started manually or some services will be disabled so when you are changing the start type of the service you have to provide the input as whether it is manual automatic or disabled similarly any other process or any other object that you talk about in computing may or may not require some input while invoking the method while taking the action on that object when we talk about the process there are multiple processes currently running on this computer when you run this command get process it will show you list of all the processes running if you want to kill a process you just need to invoke that method to kill the process it will eliminate that process or it will stop that process but when you have to do something else if you want to invoke some other method that requires some input you have to provide that input any questions about objects then there is something called as event anybody knows what is an event in context of objects when the state of the object changes that is called an event for example there is a very thin line between a method and an event restarting your phone is an action but during the restart there are certain events like before restart after restart before restart is an event if you want to do something before you restart your phone like shutting down the applications or putting some services to sleep mode till you restart or after restart immediately after restart if you want to perform some actions you can put those actions inside the event but in power shell we seldom make use of events we always look at properties and methods events are useful when you are doing dotnet programs when you are writing c-sharp code that time you can better manage events but not in power shell so event is just for your information but we will focus on properties and methods okay now let's see few examples get-service just to keep this list short I will use app star it is showing me list of all the services where the name begins with app so by default it is showing me only three columns but that's not everything that is available about the service object in the output if I want some other information about the services I can use select object and specify which properties I want to see from this object using hyphen properties I want to see first the name then display name then status I am adding one more property here start type whether this service is set to manual start or automatic start or disabled now the question is from where did I get this for property name start type how do I know and what else could be there similar to start type what other properties can be there that thing comes under object manipulation manipulating the output of your command that is the third important topic that we are going to discuss so when you look at start type this property or there are certain other properties like required services there is a typo required now the question is from where do I am getting these property names that is because I have used the these property names earlier that's why I know but how will you identify all the properties which are associated with this output how do you find all the property names so there comes the third important command that we are going to learn today get hyphen member now when you run this it will not show you the original output of get service it will show you as a result of get service command let what type of object you are going to see so here as a result of get service command let the object type that I am getting is system dot service process dot service controller this is the output object type now it's like a person's name is father's name then his family name system is their family name service process is the father's name and the child name is service controller so we don't call somebody with his full name always we always call a person by its by his first name right so we will use the object type as service controller we'll call it as service controller so get service command gives me a service controller object and if you look at the remaining output first column talks about what are the members available for this object type and second column talks about what is the member type so get service command let gives me a service controller object which has certain properties and methods so here anything that has method keyword in the second column whether it is a script method or any type of method if it is having this keyword method that means it is a method and using which I can do something with that object for example I can start this service I can stop the service I can refresh I can pause I can continue if a service is paused these are the actions that I can take on the service controller object and anything which has the member type as the keyword property will have some value so whether it is alias property not property script property or property if it is property that means it will have some value associated with it methods will not have any value methods will be used for taking action on the object it will not give you any value in some cases methods will return some value but not always if the method if you look at the definition if it begins with void that means this method will not give any output but if it shows some like system dot object or it shows string or system dot runtime dot remoting dot object reference that means that method is going to return some value but we are not concerned about the written values as of now where it shows get hash code it returns an integer value methods are used for taking some action on the object properties are used for reading the values for that particular property so now you might have understood where that start type required services and everything which I have tried is coming from it is from the properties I have a start type property I have you will find it required services there it is an alias property for what required services is equal to services depended on so here if you select services depended on or you select required services it is going to give you same results because it is the same property with a different name you can try different properties with select object and it will show you the values for those properties but if I want to see everything I can use select property star it will show me all the properties and their values so in power shell whenever you select more than four columns it will automatically switch to list view if you still want in table format you can further pipe it to format table that is the default behavior any questions about get member that comes under filtering okay if you want all the services where the start up type is but if when we look at the current results all the services start up type is manual so we need to increase our scope by maybe removing this app star let's use a star so there are certain services which are automatic certain manual if you want to filter it you can pipe it to where if an object and here inside curly bracket you need to specify where dollar underscore now this dollar underscore in represents the current item in the pipeline we have not discussed about the pipeline yet that is the next topic dollar underscore represents the current item in the pipeline because when you run this command it is producing so many service controller objects right how do you identify each individual object there must be some symbol using which you should be able to identify every single object which is coming to represent that incoming object so dollar underscore represents every item which is coming from the pipeline so here I want all the services where start type is eq means equal to automatic I need to close this curly bracket automatic you can try it without double quotes also no it will not accept either double quotes or single quote so there is a different meaning for double quote and single quote but in this case it is same how does that make difference we will see later yes you can put auto star it will search for all the services where the start type begins with auto and it might end with anything that will be useful when you not in case of start type suppose you are looking for say display name where now there is an alias for where object that is question mark it is same as where object and inside curly brackets dollar underscore dot display name here instead of equal to you can use like if you want to use star display name like auto star why okay there is nothing as auto application anything where display name is like application and it can end with anything any other questions let us see how to sort so since we know the property name you can use it for selecting the properties or you can use it for sorting the output for example if we go for get hyphen process it will give me list of all the processes what if I want the top five processes which are consuming highest amount of CPU from my computer pipe it to sort or sort object both are same sort is an alias for property CPU in descending so that I will see the highest ones first and select first five if I want top five these are the top five processes which are consuming higher okay so when we ran this command get service for the first time it gave us the list of services that we have asked for and the details in the output but when we started those services after starting we have to repeat that command get service to see the output right because start service command is not supposed to produce the output it is just meant for starting the service it's not a get service command it is start service of command now after starting the service if you want to see the output again without running get service command that means you are forcing the start service command to let those objects flow further in the pipeline you can use pass through pass through means let the objects pass through the pipeline further so it will produce the output immediately after starting those services now this can be used further suppose you want to restart the service one or multiple services if you want to restart so what you can do is what is the steps for restarting the service first you have to stop it then I you have to start it again right so you can use stop service command let the objects pass through the pipeline and give start service again let the objects pass through the pipeline so that I can see the latest status of these services when I hit enter these services are first stopped and then these are started so this pipeline has taken care of handling these objects in a sequence in the pipeline so this first command get service has produced two objects those two objects are passed to the next command that is stop service stop service has performed the stop operation on these two services and since it has the parameter pass through it is allowing those objects to flow further in the pipeline and the next command that is the last command start service is again receiving these two objects for performing the start service operation and again since we have passed through the objects are trying to flow further in the pipeline but since there is no other command in the pipeline it is showing the output on screen so when the commands object the output object is flowing in the pipeline and when it hits the end of the pipeline the objects will get converted to plain text and will be displayed on screen like this if you don't use pass through it will not go I will show you the example again ok now here we are using this pipeline for first stopping and then starting right what if I don't use this pass through what do you think will happen yeah it will stop the services but start service command let will not receive any input objects so start operation will not complete no output if I check the status of these two services these are remain stopped because I did not allow them to pass further in the pipeline from stop service after stopping those objects were dropped in the pipeline because I am not using pass through so start service command let did not receive any input objects so it did not start pass through you will use in all those cases where you want the object to get passed to the next command in the pipeline it will keep passing only till it hits the command which is not supposed to produce any output stop service is the command which is by default not supposed to produce any output unless you use hyphen pass through ok the commands which are mean for taking some action on the objects usually those commands are not producing any output like stop service restart service but the commands like sort object where object sorting filtering these will produce output so somewhere in the pipeline if you are using sort object you don't need to use pass through it will automatically pass the output to the next command in the pipeline any questions are we all clear ok let me show you how objects flow in the pipeline in a slow motion I will artificially add some sleep maybe one second before each object is shown in the pipeline ok so in order to deal with the items in the pipeline get service a star let us see these many services ok let's reduce it further AP star these many examples are enough to show you how objects flow in the pipeline now if you want to visualize how objects are flowing in the pipeline you can pipe it to for each object I want to perform some process on those objects here comes the this is called script block this curly brackets these are script blocks here we can visualize every single object which is coming into the pipeline how it is being handled by putting a sleep start iPhone sleep seconds let's put one second semicolon will terminate this command here itself and will allow us to put the next command in the script block now write host current or this write host is optional you can directly given double quotes current service in the pipeline now here I want to print the name of every single service of object which is coming in the pipeline so for that inside these double quotes I need to use the syntax dollar parenthesis and every single service object will be represented here using dollar underscore dot its property which property I am looking for I am looking for name property current service in the pipeline is I am printing the name of that service it will wait for one second and after one second whatever objects are coming in the pipeline it will give a one second pause and will print the name of that object now you can visualize if we don't give this pause of one second it will be so fast so quick you cannot even see that it is coming one by one right it does not come at all at once this is how the objects are flowing in the pipeline got it yes see here if you don't use this for each object what it what will happen is whatever is the output of this command will directly flow here and will start coming on screen and it is so quick so fast that you cannot visualize that it is coming one by one using this for each object I am adding this artificial sleep of one second and here I am overriding the default output with the output that I want here dollar underscore is representing every single item in the pipeline also instead of dollar underscore you can use dollar underscore and dollar ps item both are same dollar underscore and ps item both are same dollar underscore is the alias or short name for ps item and if you see the output still you will get the same output so dollar underscore means the current item in the pipeline okay now next question here I am using get service piping it to stop service and start service right what will happen if I use instead of service if I use process and start process do you think this will work or let me build a new command line here get hyphen service this is giving me list of all the services if I pipe it to stop hyphen process what do you think will happen instead of stop service I am using stop process the first command is giving a service controller object and the second command is dealing with process objects in this situation whether this command line will be successful or it will fail we will find out but this command is very risky it might put my computer in hung state it might restart or I don't know what will happen but my operating system will become unstable never try such commands on the production servers server is gone for toss okay and they will find out good done that power shell provides a very safe option to find out what will happen if I do this without actually seeing what really bad happens when I do this so the parameter is what if this will just tell me what will happen if I do this without actually doing it so it is trying to do something but getting error but there are certain successful hits if you see when it is crawling so fast there are certain white lines white lines means something was successful it was able to do something successfully though most of these items in the pipeline has failed but there are still few success cases here and if you want to see only those success cases you can use hyphen error action silently continue don't show me error show me only the success it will hide all the errors and we will show only the success cases so these are the successful events what would have happened if I would have ran this command without using what if if I remove this 1 2 3 4 5 services their corresponding processes would have been killed would have been stopped but the question is why few are successful and why others are failing yes now if you read this error cannot find a process with name Xbox net API service and here it is successful because the process name and service name both are same so for every service there is a corresponding process when services are running in the background in operating system for those services there is some process or some executable file which is running in the background right but in few cases the process name and service name both are same those are the successful cases and where the process name and service name does not match these are the failed cases but the question is what is the key factor that decides whether these two commands can work with each other in the pipeline or not so that is the most important topic of today's discussion how pipeline works so there are three ways the pipeline tries to connect two commands with each other plan a is by data type if you are running a command which produces the object of type service controller so the command like get service is producing service controller when you pipe it to get member what do you see service controller and when you look at the help for stop service command or start service command the input object type required by start service or stop service or restart service these commands require a service controller type of object that means the command which is giving the output and the command which is taking the input from the previous command both are having the same type of data requirement data type is same both requires service controller object let us see how to find out so when I run get type and service pipe it to get member what is the output object type service controller so get service command produces service controller object and let us take a look at help stop service show window now if you look at the input object in the syntax let me make only the input and syntax visible here input output and syntax okay now what type of input is required service controller for stop service or start service or restart service these commands require an input of type service controller get service gives an output of type service controller exact same data type so this is the first preference power shell will see if the command which is giving the output and the command which is taking the input both requires the same data type it will give preference to plan a by data type then we have seen examples like sort object select object where object for filtering right so there in those cases when you look at the let me close this one help sort object and show window what is the input object required here ps object ps object is called generic object type when we were discussing about objects I have told you every object in power shell is a child of ps object whether it is service controller object whether it is process object the parent object type is ps object so it's like when you can handle a kid you can still handle sorry when you can handle a parent you can handle their kids also you can handle the parent object type so you can handle the child object type so for sorting input object type required is ps object select object also requires a input object of type ps object that is why select objects sort object where object these type of commands are capable of taking input from any command which is available in power shell otherwise sorting is possible only for a particular type of commands right that is not the case sorting should be possible for any type of output that is why sorting requires the input of ps object that is second that is plan B which is called by generic object then comes plan C plan C has so many terms and conditions with an asterisk like when you go to a shop you see some offer there are so many terms and conditions applied with a asterisk so plan C comes with that asterisk so many things need to be present then only plan C will be successful so let's talk about the situations where plan C will be successful so for plan C to be successful in pipeline first condition is the property name from first command which is on the left hand side should match the parameter name of second command which is on the right hand side that is first condition so in this case when we have seen it has shown few successful hits here plan C was implemented because when you look at get service get member there is a proper property called name and when you look at the second command that is stop process why it was successful we are analyzing this situation for stop process when you look at the help there is a parameter called name now the value from this property will be given as input to this parameter name because they are exactly same this is also name that is also name okay that is first condition property name and parameter name must be exactly same second condition their data type should be same what is the data type the data type of this parameter is string and what is the data type of this name parameter that is also string how do I know because it is that is an alias property for service name and if you look at the service name it is string so this is not or this is and both should be both condition should be fulfilled property name and parameter name should be same their data type should be same and third and very important this is also and the parameter should accept pipeline input now if you go here and select the parameters which parameter we are talking about we are talking about the name parameter the name parameters should accept the pipeline input if it does not accept pipeline input it will not work when these three conditions are fulfilled the output of the previous command that the first command can be taken as input for the second command according to plan C so when you will write your own scripts you will be implementing plan C but looking at the time I think we are already at 330 unless you are ready to spend more time if you do not have any other things planned I will go with your inputs before I step into the scripting part otherwise we will schedule some other time because after this I have to show you few examples how using pipeline you can export the data into different formats these things are available on my YouTube channel my YouTube channel you can search for it pobo these things are already covered but I I do not give everything on YouTube channel because I am a professional trainer I have volunteered here and I used to give trainings to corporates and individuals on a paid basis sorry scripting portion will take an hour actually I have tried to put it in a nutshell because if I show you the actual course content which I have it's quite big it's 44 hours of training so I have cut short lot of things there are so many things so I have just covered enough for you to get started there are so many advanced topics even there are few topics which are not there in this course content those are on-demand topics usually when I go for companies when they ask then only I include those topics the last topic is DSC desired state configuration wherein you can use power shell to maintain the state of a server for example in companies it is very important that whatever is the configuration of your server like there are certain services which should always be running there are certain files which should always have some configuration data in a specific manner when your server deviates from that configuration DSC will bring it back to the original state automatically if some service which is supposed to be running it it stops power shell will automatically start it without manual intervention once you define how you want that server to be like it will always be like that that is called desired state configuration that is also not that is not included in even in this course if I talk about desired state configuration that will become 50 hours course so yeah you can let me know I will just finish this part now any questions about plan C because in order to create your own scripts you will always be implementing plan C when you will develop your own power shell commands you will be implementing plan C okay now there is something called as extracting value from a single property let us see what is that when you are running a command like get-service app star it is showing you the complete objects in the output now if I don't want this complete object I just want the display name part of it there are two ways either you can pipe it to select display name it will show you the display name like this or if you don't even want that header if you just want to see the display name put this entire command inside round brackets and give dot display name this is called extracting value from a single property this feature was not available before power shell version 3 this was introduced with power shell version 3 onwards where this will be useful I will show you now for stopping certain services when I use stop service I can provide multiple service names like bits winrm I am passing two service names here and when I give hyphen pass through I can see after running this command the services are stopped now here I am passing two service names but think about the situation where I want to pass say hundred service names which I want to start stop or restart whatever and those service names are stored inside a text file so notepad inside this I have winrm bits and that's enough just to show some example let's add this one api client so assuming that in my text file I have hundred services now think about the situation should I go and type those hundred service names by giving comma is it ideal no right I should be able to read this from the text file and use it inside my command so let me first save this file save as c temp input dot txt okay now how do I get the contents get content input dot txt that gives me these three service names right how do I get these services based on these three names from the text file get hyphens service hyphen name now here I will use a parenthetical command I will read the text file inside this parenthesis get hyphen content input dot txt this is how I am reading the contents of the text file and using it as the value for that input parameter where I need to pass the service names the result of that get content will be given as input for the name parameter now if I want to start or stop these services what I can do for stopping the service hyphen service now how do I get the names as a result of this command whatever comes I want to read the name property of it and use that as the input for stop service then give pass through so that all these three services will be stopped and I can see the results now how do I start these services again I will put this entire thing inside round brackets and get the name out of it and use this name these names as input for starting the service start hyphen service hyphen name and at the end I will give hyphen pass through so that I can see the output as well now in this case the innermost parenthesis will get resolved first this is going to get executed first the output of this will go to this so here the name property of this will be given as input to this name parameter then the outer parenthesis will get resolved and at the end this will get executed so the flow of parenthetical command is always from inside to outside but in case of pipeline the flow is always from left to right here it is from inside to outside okay that every client service it is not able to start because of some reason that has some dependency so you can ignore the first error but ideally it should be able to restart the services like this now assuming that in that text file you have hundred services within one command you are able to restart hundred services at once this is called parenthetical commands when the result of some command will be used as input for the outer command did I confuse you guys yeah see there are certain situations where you will find that pipeline cannot be used okay I will tell you the situation where this will be useful suppose when you are running the pipeline pipeline cannot go back reverse right it cannot go towards left it will always from it will go from left to right but in middle of the pipeline you want to run some other command and utilize the output of that command as input for the current command like in this case we are doing so suppose in the middle of the pipeline somewhere you want to read some text file and get the contents in that case within the pipeline you can make use of parenthetical commands but there in the middle of the pipeline you cannot put another command for reading the text file and giving the taking the input from the text file there this parenthetical command will be useful see when you are using a pipe pipeline you have to read the text file as the first command if you are as a first command you are running get service command and if you are giving the output of that command to the next command and but somewhere in the pipeline you need to read the text file then you cannot do that yes yes because pipeline works on one set of output right there will be a collection of objects which will get produced by the first command it will keep on throwing those objects in the pipeline from one command to another in the middle somewhere you cannot get something new there this will be useful okay so that is about parenthetical commands I am about to show you some ways through which you can redirect the output of your commands to either text file json xml or html or even csv file csv file is by default when you have excel you can open when you double click on a csv file it will open that with excel okay now get hyphen service a star if I pipe it to export hyphen csv I have to give the path c colon backslash time because I am already in the temp folder I can directly give the path as services dot csv this time you did not see the output on screen the output is redirected to the csv file if I go to see temp and open this csv file by doing a double click on it it will by default open with excel and it will show me all the properties and their values stored inside the csv file now here required services is not giving the actual list of required services because there can be more than one required services for a service one service can depend on multiple other services how can it fit multiple service names in one single column because every service is an object and it cannot fit multiple objects in one cell right so it is replacing the actual object details with only the object type so how to deal with this situation then when you have something like this where there could be a nested object inside one of the columns you should use XML now if I import it back import hyphen csv services dot csv I am able to see everything except this nested objects right in such situations it is better to use XML format so here export hyphen clixml is the command for exporting the data in XML format services dot XML and you need to specify the depth why you need to specify the depth because think about the situation now one service depends on two other services and those two other services again depends on two other services or three other services and again those services will keep on depending some other services this hierarchy can go in a loop as well because the last service again depends on this first service then how it will decide till what level it has to show that so you need to specify the depth so I will specify the depth as three so it will go on only till three levels and will stop instead of getting into a loop no it will go only till one yeah it will not show you further dependencies and if you increase the depth the file size will be bigger and the output will be slower even with depth of three it is taking few seconds ok now if I import that XML file import clixml and services dot XML the data is still intact if I pipe it to select star see this time I am not getting that service controller object I am actually getting the real data because it has preserved everything in nested manner similar to XML there is another format called JSON how many of you know what is JSON format yeah so JSON is also similar to XML where it is representing data in a different way so I will just show you how JSON data looks like but there is no command like export to JSON there is convert to JSON if you want to save this inside a file you have to pipe it to out-file and give the JSON file name but I will just show you depth of two this is how the JSON format looks like ok you have to save this in a text file using out-file then while bringing that data back you have to use convert from JSON after reading the text file get content name of the JSON file pipe it to ok if you want I will show you out-file but there are certain issues with JSON format you will see when you import the data back services.json get-content services.json pipe it to convert from JSON now here if you look at the status instead of showing running or stop it is giving some number because that is how JSON works it will look at the actual because internally for the service status whether it is running or stop there is some number also assigned there is a corresponding number for stop there is a corresponding number for stop or running so it will convert those numbers and you have to again convert back these numbers into their corresponding values but JSON can also reserve the hierarchy it can store nested objects with the depth specified ok for get-content read the JSON convert from JSON convert to JSON specify the depth and out-file file name now the last one is HTML that is interesting and it looks very beautiful when you export using HTML and you know how to format the HTML using CSS cascading style sheets now if you convert this output to convert to HTML if you do not specify any formatting it will to a very raw HTML conversion you have to again save this out file services dot HTML when you open this HTML it will by default open with your default browser so if you want you can use select object before you convert this to HTML if you want only specific columns in the output select name display name status start type if you go back and refresh this this time you will see only specific columns that you have exported will appear here now how do you format this because it is very crude basic HTML right so for formatting this you need to know a little bit about CSS which you can put here in head if you know about CSS styling if you don't know don't worry there is a free tutorial available at W3 schools for learning CSS all CSS styling goes inside this style tag under the head of HTML now for this entire HTML I want to change the font font family aerial just to see this when I refresh font is changed now we will add table borders for adding table borders table table data and table header border one pixel solid gray and by default when you add the table borders there will be some cell spacing if you want to get rid of the spacing between the border cells you can do that by giving border collapsed as collapse okay I think I have to give it separately and one more thing here padding five pixel okay that collapse thing is I think there is some type or some syntax error it's not working collapse or collapsed I might be having some syntax error forgetting something I don't remember all the syntax okay now let's add some fancy stuff here like when you take your mouse cursor over any of those rows it should change the background color or something like that like when I take my mouse cursor over table row over color should become white and background color should become gray let's make it dark gray and for cell which is table data for hover background color should become green or when I come back here and refresh yeah you can you can read a file by creating a style instead of file and in the head use a parenthetical command get content of that file you can do that but for that you need to know how CSS works all different syntax of CSS so that's all about using pipeline for connecting commands extracting the results and exporting it to different formats any questions convert JSON to which other format see CSV you can convert when there are not multiple layers of information okay I will show you for example get hyphen service now here I if I select name display name and status only three columns let me select only few services abster if I convert this to JSON here I don't need to specify depth because I have not included the columns which can have multiple layers of information these columns will have only single layer of information single values so this is the JSON output if you want it to be converted to CSV you can first write this to a file out hyphen file services.json now read that file get content services.json convert from JSON and now you can pipe it to export hyphen CSV services.csv done now if you open services.csv you have converted the JSON to a CSV format now here okay I think I broke open the wrong services okay yeah when I convert from JSON services.csv oh it's not preserving the properties here let me just check get member what type of object it is producing it is producing an object a generic object yeah from JSON it's not able to convert to CSV because JSON is not preserving the object hierarchy all the properties and which which are available in the JSON text file it is not preserving those properties in the object sorry how to open a file which file see it depends on what type of file like if you are opening an Excel file just give the Excel file name it will automatically open it with Excel text file image see whatever file you specify here for example let me show you if you are opening a text file like this session 1.txt or session 2.txt if I copy this this file name paste it here give dot backslash and the file name when you hit enter it will automatically choose the default application associated with that file type that is one thing and like WB form this last file I am trying to open from PS ok FR ok now this is an Excel file when I just give the file name it will open by default with Excel mm-hmm two files with the same name so you have to give the complete path of that file like if the file is in a different folder currently we are in temp folder if you are trying to open a file from some other folder like c colon windows backslash and some other folder whatever folder path give the complete path and the file name it will open that file yeah dot backslash is used when you are in the current folder ok so that completes our discussion about pipeline the remaining two topics like writing your own scripts and publishing scripts we have to it will take one hour maybe we will schedule some other time if possible unless because even for writing scripts because of the time constraint I have compressed it a lot otherwise writing script has so many things that I need to explain should we give it a hard stop now everyone ok those who have some other things or some other plans you can leave if we have majority of you who are ready to stay will continue if we have very few people who are ready to continue will give it a hard stop I am ready to give but even for this session we have to plan at least I think two three months in advance should we call it a day or continue ok good ok so so far whatever we have seen it's all about running commands and unless you have a requirement of running your set of commands repeatedly multiple times a day or if you want to reuse those set of commands again in the same manner you only in those cases you have to think about writing a script otherwise if you want to do something just for one time running commands is enough but when you are working in companies where same things you are doing on a daily basis instead of typing those commands again and again you will put those commands in a script now what is a script script is just a collection of multiple commands which will get executed in a sequence when you put multiple commands in a sequence inside a file and save that file with the extension dot ps1 ps1 is the extension of PowerShell script files when you put those commands inside a file and save it as dot ps1 that becomes a PowerShell script and PowerShell scripts when you double click on it it will not run it will run only when you call that script from inside PowerShell using the command line let us see our first script so before we write the script let me show you some basic ways of writing the output and taking the input for writing something on screen the command is write hyphen host this is sample output so whatever you write using write host will be written only on screen this output will not flow to the pipeline what does that mean if I write this using write host and pipe it to out hyphen file output dot txt see that output did not go to file it is coming on screen if I open this file does not have any output because write host means write on the screen nowhere else let me show you another way of writing the output write output this is another sample output this comes on screen in the same way but if I pipe this to out hyphen file output dot txt before I do that let me close this file this time you will find the output did not come on screen because we are redirecting it to a file and if I open that file again the output is here so what is the difference between write host and write output write host will write directly on screen write output will write it to the pipeline and if there is nothing further after write output it will show it on screen if there is any other command which is consuming that output it will go there got it that is the difference between write output and write host I am just covering some basics before we start writing our own scripts then variables in power shell if you want to create a variable like variable x variable name will always begin with dollar symbol when I say x is equal to 10 and if I check dollar x it will give me the value of 10 now this dollar means what dollar means inside of x is the name of a box you can assume it as a empty container when I am saying dollar x is equal to 10 that means inside of x is equal to 10 when I am saying dollar x I am saying show me what is inside x x is the name of the variable and dollar means the pointer what is inside of x okay now when I say dollar x plus dollar x I can perform mathematical calculations on variables now when I check dollar x dot get type dot name it will tell me what type of value is stored inside x so currently it is storing integer 32 bit integer now if I store the same value by using double quotes when I say dollar x is equal to 10 but inside double quotes when I check the type now it is string how does it matter if I say dollar x it is still showing me 10 the way you perform the mathematical calculations on this when I say dollar x multiplied by 10 see what happens it is showing me 10 10 times when I say dollar x plus dollar x because x is currently string when you are storing a string it will do the string operation it is not doing the mathematical operation which it has to do on an integer right but see something interesting happening here if I say 10 multiplied by dollar x what will happen now 100 what type of operation need to be performed power shell will decide based on the data type of the first item in the equation if this first item is integer it will consider this as an integer but here this was string it is considering this as string and doing the multiplication of this string 10 times okay now let us see what is the function now we will move to the scripting part this is a script editor pane where you can write the script whatever things you write in this file will get executed in sequence line by line one after another okay now here if I put dollar x is equal to 10 then I am writing this write output the value of x is dollar x in power shell case does not matter lower case upper case you can use any case everything is same if you create capital X here and use small x here it will consider it as same now when you save this sample script it will automatically put the extension as dot ps1 now when you run this value of x is 10 now if I put this inside a function now what is the function suppose you have certain lines of code that you need to use at multiple places suppose you have created a function for doing some calculation based on some given input values may be doing multiplication or subtraction or addition you can create that function which is taking two values as input and when you have to call that function you need to provide those two values as input then only that function will return some value but here if I run this script now I do not see any output why because I have defined a function but I have not called that function anywhere in order to call that function once you have defined you can call it either within the script or you can call it from command prompt also now if you check here test function I can call it from command prompt also or if I want to call it when I run the script I need to call it explicitly here like this you need to call the function by its name then only the code inside that function will get executed if you do not call that function the function is just defined but it is not going to get executed now if I run this I will see the output okay sorry can you please be a bit louder sorry I am not able to text test function you have to call it by its name then only it will get executed you mean from the command prompt okay once you register it it will be globally available you can globally means for that power shell session that function is always available see when your function is inside a script you run that script it will get registered for this session you can call it from the command prompt as well but if I close power shell now if I start power shell again see that function which I have created was available for that session now if I use test function it doesn't work functions are available only for that session now if you want that function to be available again you need to open that script and run this function again the moment even if you don't call that function within the script when you run the script since this script is executed now as you can see this function is available again am I clear in your case it is not working yeah you you have to run that script once again sorry oh yeah so if running script is disabled on your system then I will show you one command which will enable it those who are getting running scripts is disabled on your system you can run this command set hyphen execution policy remote signed say yes to all then try again set execution policy hyphen execution policy remote signed we don't have enough time to explain about execution policies it's a big discussion okay that depends on yeah once you set it it will remain there but certain operating systems have restricted policy by default but windows 10 latest version I think it has remote signed by default now you are able to run it yeah okay so that is how a function works you have to define it once but you have to call it in order to make it work just defining the function will not actually execute the code inside that function unless you are calling that function now let us see a function which can accept some values I will write another function add numbers now to this function I need to pass two numbers and it should return me the addition of those two numbers so for passing the values to a function you have to use param block it is also called parameter block it is written as param dollar x dollar y and this function will return with return keyword it should return dollar x plus dollar y the addition of these two numbers now if I run this file in power shell I have a function available now with these two parameters x and y I can pass the values and get the results add hyphen numbers I can use hyphen x to provide the value for x something hyphen y something and it returns the addition got it now what if I provide some text here how do you take care of the situations that user should not provide wrong values how do you specify this parameter should be an integer so here before the parameter that variable name in square bracket give the data type and this is how you can force it to be an integer okay before I test it let me put the value of y as 10 here and value of x I am giving as again string it is showing me 2 0 1 0 because I have given the numbers inside double quotes but the changes which I have made I have not saved yet I will save this and run this sample script again now this time inside the script I am forcing these parameters to become integers because I have put int in front of the parameter names if I try this again this time it is giving me the value of 30 because I am doing that forceful conversion to integers inside the param block okay next now we are going to jump to few levels up wherein we are going to create a function which will behave as a power shell command so before we see that example let me show you how to create a dictionary with key value pairs in power shell for creating a dictionary which behaves as key value pairs for example person info or let us call it as person is equal to at the rate curly brackets now we are creating a person variable which will give us information about a particular person so that information will be name is equal to John age is equal to 36 country is equal to Singapore now there is a variable which has these key value pairs here whatever comes before is equal to sign that is a key name and this is the value for that key this key name cannot be repeated for example here if I again add another key with the same name before even I hit enter it shows me that something is wrong with this when I take my mouse cursor on that red underline it shows duplicate key country not allowed in hash literals this is also called a hash table so I will change it to city now that error is taken care now when I do dollar person it will give me all the key value pairs now if I want to find the person city I can give person dot city it will give me what is the city the value for the city property for that person okay now we are going to write a script which is going to give us details about my computer what is the operating system how much is the RAM things like that how to get details about the computer system in power shell there is a command called get hyphen w my object hyphen class win 32 underscore computer system this will give me various different properties of the computer system like domain manufacturer model name of this computer primary owner name total physical memory like how much is the RAM available on this computer so what I can do is I can store this information in a variable dollar CS means computer system info CS info is equal to when I store the result of this command inside this variable similar to get service by default you were not able to see all the properties right even for this command there are so many other properties which are by default not visible on screen you can find it using get member or when you are storing it inside this variable all those properties and their values are also stored when you do dollars CS info dot here it will show you all different properties and their values you can select the properties that you want like if you want to find how much is the RAM use dot total physical memory this is showing the value in bytes there is another class called win 32 underscore operating system that gives me operating system details dollar OS info so instead of computer system I will use operating system now when I check dollar OS info it will have the details about operating system now what we can do is we can write a function get system information to which you can pass a parameter called dollar computer name and it will show you the hardware and software details of that computer now when you look at it these are two different variables CS info and OS info CS info is giving the hardware related details OS info is giving the software or operating system related details how do I combine these two things in a third object which will give me collective details about hardware and software both so for that I will create a hash table or a dictionary which will in which I will combine these two values okay so I will create another variable called dollar CS info is equal to add the rate curly bracket inside this let's combine properties from these two different variables together and create a new variable and when you are writing it inside script just to make it more readable you can put it like this like PC name is equal to dollar OS info dot PS computer name this will give me the computer name then OS name what is the operating system available on this computer I will get it from dollar OS info dot caption this will give me whether it is windows 10 windows 7 whatever let's add how much is the RAM RAM is equal to dollar CS info dot total physical memory and which property was giving us the details about domain it was computer system domain is equal to dollar CS info dot domain that's it we will just keep it short so we are combining the values from two different commands results and we are creating another hash table that will show us the results in a cumulative way so at the end what we will do is write hyphen output dollar CS info now when user does not provide any value for this computer name parameter we want to default it to the local system so you can do that by giving is equal to local host when someone is not giving any value for computer name it will be considered as local host but if somebody gives some other name it will replace that local host with the provided value this is called the default value and this computer name is going to be a string so I will put it as string now how do you get the values for this particular computer you can use hyphen computer name dollar computer name and even here computer name dollar computer name now we have created a function which will accept a computer name as input parameter and will give all the details of that provided computer and if no computer name is provided it will give the details from the local computer when I run this now I have got another function registered here get hyphen system information system information now I will hit enter without providing any input it is by default giving the details for local host instead of giving error otherwise it will give an error that computer name is null right now if I want I can give the computer name as it pogo still it will give me same results now here if you are in a network and you give a remote computer name it will give you the details of the hardware and software configuration of that remote computer got it okay now how do I pass multiple computer names here that is next step before that when you look at the output of any command in power shell does it show you name and value like this it does not show you name and value it will just put this OS name RAM PC name and domain as the column addings and the values these values will be shown below that right for that you need to know how to convert this object this dictionary into an object so for converting this dictionary into an object we have to again reassign this dollar sys info is equal to new hyphen object type name ps object and properties will be taken from dollar sys info now if you want instead of reusing the same variable you can create a new variable also like out object and instead of giving sys info we will give out object now if you try this command once again these key value pairs will be converted to an actual object got it now if you want this RAM to be shown in gigabytes it's very simple in power shell you can put integer in front of this and divide this by 1 gigabyte I am putting that integer to get rid of those decimal places just divided by 1 GB it will show the results in GB now RAM is 12 GB now next is how do you pass multiple computer names here to accept multiple computer names the parameters should have empty square bracket right like this but you also need a logic using which you can handle those multiple computer names one by one so in power shell there is something called as for each dollar computer I am creating a new variable called dollar computer this new variable will hold every single value from this collection which collection in dollar computer name for each computer in computer name I want to repeat these lines so I always give an extra level of indent for any code that goes inside curly bracket that is not a rule but that makes your code look more professional and more readable now here if I pass only one computer name then well and good it will show only one result when I pass multiple computer names for each single computer it will repeat this entire process let me run this again and this time I will pass multiple computer names so I am passing comma separated multiple values so I will see 3 ok system dot object ok here computer name yeah here we have to change this instead of using it against a collection we have to use it against the current computer from that collection because see this computer name is going to come with multiple values and here while connecting to the computer we cannot give multiple computer names at once we have to handle it one by one each computer in one iteration now it is fixed it is giving me results for three different computers this way now I can pass thousand hundred how many computers I want so my command is now capable of handling multiple items my function can handle multiple inputs and will run the code for each single item which is given on given as the parameter input and show me multiple results any questions everyone understood how it is working now this command that we have developed just now it looks like a power shell command but it is not yet capable of handling pipeline input what does that mean suppose I want this computer name to be passed from the command from the previous commands result if I want to read this computer name suppose I have a text file notepad input dot csv so here I have a column called computer name which has the values of it pogo look local host and it pogo now if I import this csv I am getting an object which has a property called computer name and three values under that property right if I pipe this to get hyphen system information my command which I have developed is not yet capable of handling the pipeline input next step we are going to see now here it is showing the output for the default local host it is not reading the output of the previous command it is taking the local host how do I make this script capable of getting the input from the pipeline using which plan plan C so what does plan C say the output object property from the first command in this case import csv has this property computer name should match with the parameter name again that is computer name first condition is fulfilled but remaining conditions data type this is also string this is also string second condition is fulfilled but third condition is not fulfilled yet that is that computer name parameter should be accepting value from pipeline how do you make it accept value from pipeline let us put this in a new line to make it more readable because the amount of code that will go will increase now to make this parameter read the value from pipeline you have to put in square bracket parameter value from pipeline and value from pipeline by property value by property name two things once you put your command is now capable of accepting value from pipeline that is first step second step is this entire code will go inside process block because you are processing the input from the pipeline the entire code will go inside process block okay let me show you the complete thing once this is how the code will look like so we have added this parameter value from pipeline value from pipeline by property name and this process block to make this command capable of handling input from the pipeline now here we are implementing plan C and what are the requirements for plan C the output property name from the first command so in this case what is the output property name of the first command computer name should match the parameter name what is the parameter name that is also computer name then this can go as input to this let us try that now here with get system information I am not specifying anything it will automatically read the values from the previous command okay I did not save this I have to run it save it now see here did I mention that which three computers I am talking about no it is reading the output of this as input for this so we have seen how to implement plan C using a script now if you guys want I will send the script to Chaitanya on email he will share this with you okay I think that is enough for today even remembering all these things at one go will be difficult right sorry see when you select like if I select these multiple lines if I give shift tab it will indent back if I give tab it will indent forward now there are so many other things as I mentioned we cannot fit it in this time frame so I believe this will be helpful for you to start using PowerShell and this is the best tool currently available on windows platform for automating stuff any questions and also you guys can search for my youtube channel it pogo on youtube you can search for this keyword okay I will be uploading few more videos for PowerShell I work on excel macros like automating excel related stuff I work on CSS3 jQuery HTML5 I work on C sharp I work on VB scripting anything related to windows system administration and automation I can help you with that no industry has mix of both of these things there are lot of things nowadays where Linux is dominating but still Microsoft has a very big chunk of market they have lot of things where Microsoft is dominating and as far as I heard or I have seen Azure is the windows cloud service which is also having a major share in the market for managing Azure related stuff you can make use of PowerShell and using PowerShell you can develop tools for automating all Azure cloud related stuff even in Visa if we talk about the number of servers I think we have somewhere around 80,000 plus servers out of which 50,000 windows servers are still there but yeah Linux has become popular because it was open source but now Microsoft has also started keeping their source code open they have also become open now any other questions yes you can do that I have those scripts for that you need to have excel installed on your system where you want to do that then there is something called as com object for Excel the com object name is excel dot application dollar excel is equal to new object iPhone com object excel dot application dollar excel dot visible is equal to true see I am I am not touching anything apart from PowerShell from PowerShell command I have started excel here see I will keep it side by side so that you can see now if I want to add a workbook here dollar WB you can give it any name is equal to dollar excel dot workbooks dot add I have added a workbook if I want to type something in that cell I need to first find the reference to the sheet because there is only one sheet I will call it as SH is equal to dollar workbook dot sheets dollar sheet dot cells row number one column number one dot value is equal to test now think about it you have created a PowerShell script which is giving you some values now you want to iterate through all the values and write those values in the excel then through PowerShell itself you can create excel file write the values and you can even save that excel file for saving you can use dollar workbook dot save as I will give desktop test workbook dot excel as 6 now see here title workbook is saved on desktop now you can close it dollar WB dot close dollar excel dot quit done this is only one example you can do this with internet explorer if you want to open internet explorer go to some website fill some form add values submit the form and suppose you have in excel data for say 1000 users that you want to create on some website open excel open workbook read the values open internet explorer com object go to that website fill in the values by reading testing and then submitting the form you can do this for 1000 times programmatically yeah if they have capture you are gone that is why they keep capture there sorry yes see before you can write a program for PowerShell to fill in the form you need to first go and study the form what are the component names because every HTML component will have some ID you need to find out those IDs and in your code you need to write the IDs for each text box where you want to paste the value so I have done few automations where it was 100% automatic without any manual intervention it will generate reports send emails create PPTs from the report whatever you have generated from the information that you have collected from multiple servers it will generate excel files it will generate PPTs put those some pictures in the PPTs create graphs paste those graphs in the PPTs and send emails earlier people used to spend hours every day for doing this job this is called non-value add even a labor layman who does not know anything about this if you give them a written document how to do this they can also do that then where is the value that you are adding why you are spending your productive time in something which is not productive right for these kind of things you should think about automating but nowadays there are so many things that we are automating if something goes wrong on a server some service goes down then there will be a monitoring system which will send alert to our system that on a particular server this service went down as soon as the alert comes we already have a power shell script written it will read the server name from the alert it will read the service name from the alert it will connect to the server it will start the service and it will update the ticket that the problem is solved. So gradually non-value add things will be taken out from market lot of administrators and lot of people who are doing these kind of jobs they will lose their jobs because companies are moving towards automation now the automations that we are currently using at visa is like automatic patch installation because on production servers if you install a windows patch that requires a reboot and if some production server reboots then think about it you are using the visa servers if somebody is swiping the credit card his transaction will be declined right. So you cannot rely on manual methods of doing this you have to automatically manage everything to bring maximum uptime for your servers and make things efficient okay. See currently for all the windows servers we are using power shell scripts but there are non-windows platform like linux and network devices for that we are using python because on windows servers python is not by default available on linux python is always available okay the automation that we are doing it has multiple layers so there are few certain layers which are common across all different platforms like there is a layer called orchestrator what orchestrator does is no matter which programming language in which you have written the script orchestrator's job is to call that script based on the requirement. So orchestrator will automatically decide if it is a linux server it will call a python script if it is a windows server it will call power shell script so orchestrator is a different layer then depending on what functionalities are available for that particular platform you need to either write it in python power shell shell or there are multiple other options which are available. So you cannot rely on one particular scripting technology or one particular technology for automating you need to mix and match all these things it is a combination of all these things okay thank you for joining guys if you yeah thank you if you want you can note down my contact details I think this is not visible let me change the font for this color this is my email this is my phone this is my whatsapp number if you have any questions about scripting or automation you can get in touch with me yeah thank you thank you for giving the opportunity.