In this post we will continue discovering Microsoft’s powershell features. We talked in a past post that Microsoft created so called alias names for commands in powershell. This thing helped a lot because sometimes it is hard to remember the whole name of a command especially if it’s a command that you do not use very often. One example is the command Get-ChildItem, the command has inbuild aliases, ls and dir. Another command is Clear-Host which has cls alias . We can do the same thing with any command in powershell by using the command Set-Alias. I will show you an example of this command. If we want the Get-Command to have an alias named getcom we would type the following Set-Alias getcom Get-Command. Now if we simply type getcom, the Get-Command will be executed by powershell. Here is the output of the command:
OK, now let’s talk a little about piping or as you will get used to ” | ” . Piping is used to redirect the output of a command to another command for further processing. More about piping you can find out by typing Get-Help About_pipeline. Here is the output of the command:
It is hard to know all the cmdlets that exist in powershell. There are over 100 of them and to know the name of every one or what they do is very hard. I will try to write most of them and a short description of every cmdlet. Probably I will return again to read this beocause it will be very useful :). OK, now let’s type the following: Get-Command. Hmm… the output doesn’t look very good because it contains cmdlets,functions, aliases etc. If we type Get-Command Format-list * we will get the output of the same command but also things like Modules,Parameters,Command Type etc. The output of the command looks like this:
Now let’s try to export the output of the Get-Command in a file but also with explanation of every command. Normally to get information about a command we would type Get-Help “command name”. For example Get-Help Get-ChildItem. If we want to get information about every command we would type Get-Command | Get-Help. This means that first powershell will execute Get-Command and the output will be send to Get-Help command. Now to export to a file type something like Get-Command | Get-Help | Out-File “path”. In my example I typed: Get-Command | Get-Help | Out-File c:\Powershell\information.txt
Now if I go to that path I should find the file named information.txt:
Now if you open that file you will see that every command is explained in detail:
If I type now Get-Help Get-Command I can see that in the output of this command is a Option called -commandType, we need only to display the cmdlets so the command that we need to type is Get-Command -commandType Cmdlet. The output is this:
OK, now we have the full list of the cmdlets commands but we still don’t know exactly what every command is doing. For a command, if you want to know what its purpose you just type for example Get-Help Get-Command. In our case we only want to display the name of the cmdlet and the description. This is where Format-Table cmdlet get’s in action. Let’s type Get-Help Format-Table. If you don’t remember the command you can always search for it’s alias name simply by typing Get-Alias. If you want to find more about that command type Get-Help Get-Alias. The output of Get-Alias looks like this:
For a simple way of getting to the aliases let’s export the alias names in a txt file : get-alias | Out-File c:\powershell\alias.txt
Here are the existent alias names:
The output of the Get-Help Format-Table command looks like this:
As you can see from this image that the Format-Table command is used to alter the output of a command. So in our exercise we want to extract from the output of the Get-Help command only the name and the description of the cmdlets. So now we type Get-command -commandType Cmdlet | Get-Help | Format-Table Name,Description| Out-File C:\powershell\allcmdlets.txt
This command will export in a file called allcmdlets.txt, all the cmdlets with their name and the description:
As you can see the file has everything we need except that powershell has truncated the description.
This happens because powershell detected that the description of the cmdlets has exceeded the maximum characters allowed by powershell. A simple solution is to put the -wrap parameter in the Format-Table. You can find more about -wrap parameter by typing Get-Help Format-Table -detailed:
The parameter -wrap does the following : “Displays text that exceeds the column width on the next line. By default, text that exceeds the column width is truncated.”
Now if we return to our exercise, the whole command is the following:
Get-Command -commandType Cmdlet | Get-Help | Format-Table -wrap Name,Description| Out-File C:\powershell\allcmdlets.txt . This command is showing the whole description of each cmdlet. I think that in our case better is to have the Synopsis rather than the whole Description.
So let’s type again Get-command -commandType Cmdlet | Get-Help | Format-Table -wrap Name,Synopsis| Out-File C:\powershell\allcmdlets.txt. And finally here si the output:
OK, that’s it for this post. Stay tuned because in a future post we will get deeper intro the powershell scripting. I hope this was a nice post please comment if you liked it.