Saturday 8 October 2016

3 Important PowerShell Cmdlets – Part 2

In my previous blog, we saw a quick introduction to Windows PowerShell. Now let’s run through some of the important cmdlets.


Get-Help

Get-Help is the most important cmdlet. Microsoft has made lot of efforts to make Get-help a super cool cmdlet. This cmdlet gives you all information that you need to know about anything with PowerShell. If you feel that you got struck somewhere while working with PowerShell, Get-Help is the best place to look for your answers.
Here is the portion of Get-Help cmdlet output which explains how to use the cmdlet
































Now let use the Get-ChildItem cmdlet with Get-Help. This is just the basic help content for Get-ChildItem.


















Here are the few important things which you should look at

  • Two sets of syntax are called as parameter sets. So we have two parameter sets for Get-ChildItem. 
  • The text which you see after a dash (-) is called the list of parameters available for the cmdlet.
  • Reason, why we have two sets of syntax is, if you look at the first set you've got -Path which is not available in the second set of syntax. And in the second set you have a parameter called -LiteralPath which is not available in the first set. So that’s the difference between those two set of parameter sets. Key thing to note is you cannot use both -Path and -LiteralPath parameters at the same time.



























  • Take a look at the square and angle brackets. Below is the portion of syntax output of Get-ChildItem help.

Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Include <string[]>] [-Exclude <string[]>]

<string> -> This defines the datatype used by the parameter
<string[]> -> [ ] with the datatype indicates the parameter can accept multiple inputs
[-Include <string[]>] -> Parameter within [ ] means its optional parameter (Not mandatory)
[[-Path] <string[]>] -> Parameter defined under [ ]. In this case we have -Path and -Filter within [ ]. These parameters are called Positional Parameters. You need not mention the parameter name for positional parameter when passing inputs





















If you look at the above code I have used asterisk ( * ) represents the wildcard which you can use to filter or search for a keyword.

At last you have the aliases information.

There are different parameters which you can use. Let’s see some of them

Get-Help Get-ChildItem -Full
























Some of the key things to look under the parameters section. I have just highlighted 3 things, but that doesn’t mean other stuffs are not important. Just to start with, first, we can concentrate on those 3.


















Get-Help Get-ChildItem -Detailed -> will give you description about each parameter

Get-Help Get-ChildItem -Examples -> will list you a set of examples which you can refer

You also have an option to get the output to a user interface window. Just add -ShowWindow switch to do it

Get-Help Get-ChildItem -ShowWindow




















Get-Command

This cmdlet helps you to discover the list of cmdlets available. It’s kind of a dictionary of PowerShell cmdlets. Using Get-Help let’s see what parameters this cmdlet can take.















Let’s do a search using the -Name parameter and see if we have any cmdlets related to service.

Get-Command -Name *service*















So now we've got all command types which has a word service in its name. You can see all cmdlet and function follow a standard naming format which is verb-noun.
We got a cmdlet which is Get-Service. By looking the name itself, you can just imagine what could be the output. Let’s run and see what it gives

Get-Service


















Okay, as expected it gave us the output of list of services available in my machine. It printed status, Name and DisplayName. But if you open services.msc, we have lot of other information like service startup type, description, dependent services and all other stuff.
PowerShell can also give you all that information. Get-Member is the cmdlet which allows you to explore more property and methods for a cmdlet.

Get-Member

This cmdlet gives you details about cmdlet property and its methods. Let’s explore Get-Service cmdlet to see what else we can get from it

Get-Service | Get-Member

| -> This is called pipeline. I will cover more about this in the next blog. On a short note, the output of first cmdlet is passed as an input to the second cmdlet. In the above command output of Get-Service is sent across the pipe as an input to Get-Member.























Now we will filter the output to display only the properties for Get-Service cmdlet

Get-Service | Get-Member -MemberType Property
















We have got a set of properties which can be displayed in the output. Now let’s see how to display other properties in your output. To keep it simple, we will just use services which has win in its name.

Get-Service -Name *win*












Now we will write the code which will display only Name, Status and dependent service 

Get-Service -Name *win* | Select-Object Name, Status, ServicesDependedOn











First, we filtered services with *win* in its name. Then we piped the output of that to next cmdlet called select-object. That’s simple, isn’t it?

Instead of select-object, you can just use select (select is the alias of Select-object)















If you want to get all the details, just do a select * (like how we do in SQL Server; select * from table).

Get-Server -Name WinRM | select *






















In the next blog, we will discuss more about pipelines and filtering output


Cheers,
Naveen

Sunday 2 October 2016

Introduction to Windows PowerShell – Part 1

Windows PowerShell is a windows command-line shell designed especially for administrators. PowerShell comes with interactive prompt and integrated scripting environment. PowerShell is kind of very much advanced version of your windows dos. PowerShell is a powerful scripting environment where you can do all sorts of things from managing files, users, exchange, active directory, SQL Server and the list goes on. This powerful tool is built on top of .NET Framework CLR and .NET Framework.

Learning PowerShell will make your life easier. Just imagine you have been given a task to update a set of property for all the users in the AD or you need to pull a report which has information about all the servers in your network like IP address, CPU, RAM, storage and list of services etc. Performing these kind of tasks manually will take hours of your time to complete it. Believe me using PowerShell you can do it with just few lines of code in very less time.
I have decided to write a blog series which will be helpful especially for DBA’s who loves to learn PowerShell. This is the first blog post in this series, we will see how to open PowerShell and run few basic commands.

Let's get started!!

How to open PowerShell ?

There are different ways to open PowerShell. Launch start menu and just search for PowerShell..







































Press Windows Key + R -> Type PowerShell and hit enter















If you open PowerShell, you will land up in a blue screen command line interface. Now let’s run some basic commands which we use to run in dos. Please note most of the command which you run in dos will work with PowerShell. 


























In Windows PowerShell we have a concept called as cmdlet (command-let). PowerShell has more than hundreds of cmdlets and you can also make your own cmdlets too. All cmdlets have a standard naming format verb-noun format. Now let’s see how get the list of cmdlets and find out what version of PowerShell you’re running

Get-command will give you list of all cmdlets.










$PSVersionTable gives you the version of your PowerShell

















Now we will see how to get the list of folders from a given directory using the actual PowerShell cmdlet. 

Get-ChildItem is the actual PowerShell cmdlet to list the folders.

















So now we saw that dir, ls and Get-ChildItem all gave us the same output. Let’s see how things are linked with each other. PowerShell has a concept called alias using which you make your code shorter by just calling the aliases. 
Get-ChildItem has three aliases. Now the question is how to get the details about these aliases. PowerShell help is the best place to look for information about any cmdlet and its always recommended to update your help on regular basis. How to update help, I will cover on different post.

So now let’s see what’s there in help for our Get-ChildItem cmdlet.

Get-help cmdletName is the command which will give you information about the cmdlet.




















There are ways to get the list of aliases for a given cmdlet. I will cover that in a different blog post.
In the next blog post we will see some more cmdlets and will focus on how things can be used in doing administrative tasks. I will be covering more specifically from server and database administrator’s standpoint.

Cheers,
Naveen