If you have just decided to learn PowerShell, and don’t have any prior knowledge of scripting or programming, your first meeting with it can be quite overwhelming.
This blog post is written for the absolute beginner that has no idea where to start. If that is you, please read on to get a jump-start to PowerShell!
What if I told you that you really only need to memorize three (3!) commands to be able to fully explore the whole of PowerShell? I can understand if you are hesitant to believe me, but read on and I’ll show you it’s true.
There is however one additional piece of information you need to know to take full advantage of these commands, and that is the naming standard for PowerShell commands.
All PowerShell commands following this simple structure:
<Verb>-<Noun>
For instance, the very first command you will learn is called Get-Help.
PowerShell has a very good built-in help for each core command (called cmdlets), and most tool-builders also supply extensive help and examples to custom functions that you might find online. It shouldn’t come as a surprise then, that the first command you need to learn, is the one to show the built-in help information in all PowerShell commands.
Get-Help (alias: help)
Armed with this cmdlet you can figure out how to use almost all commands in PowerShell.
The syntax is really simple:
Get-Help [name of command]
Out of the box, an alias (‘help’) has been created for the Get-Help cmdlet, so if you want to save a few key strokes you can just write ‘help’ instead of ‘Get-Help’. By default an abbreviated form of the help information is shown, but the Get-Help cmdlet supports several parameters to help you get at the type of information you need. Two of the most useful parameters are:
- Examples (Will show only example of use)
- Full (Will show all the available help information)
You can even use it to look up help about itself. Try it out:
Get-Help Get-Help (or just 'help help')
In addition to help about the available commands, PowerShell also has a set of help topics that are about language constructs and other related concepts. Read my quick tip about getting all “About” help topics to learn how to explore these.
A small note regarding the help information in PowerShell; if you don’t get any, or very limited, help information when using Get-Help, you need to run the command Update-Help. This will download the most up-to-date help from the internet. Make it a habit of doing this once or twice a year to get any updated help downloaded to your workstation. Also, you might need to run PowerShell as administrator before running Update-Help.
Get-Command (alias: gcm)
The next command you need to know is Get-Command. With this command you can explore all the different commands that are available to you in PowerShell. It will only show commands that are actually installed, so the results might vary from environment to environment.
This command supports wildcards so it’s really easy to search for commands you are after.
Examples:
Get-Command Get*
This will list all commands that start with Get
Get-Command *wmi*
This will list all commands that contain the text ‘wmi’ somewhere in the command name.
Get-Command also supports a Module parameter that lets you list all commands in a specific module that is available in your current PowerShell session.
Example:
Get-Command -Module Microsoft.PowerShell.Utility
This will list all commands in the module called Microsoft.PowerShell.Utility
Get-Member (alias: gm)
Now that you know how to find commands, and how to look up how to use them, the last thing you need to know is what you can do with the results returned after you have run a command.
As you might know, PowerShell is an object-oriented language, and all results returned are objects. You can look at these objects; and by using Get-Member you will see all properties and methods that belong to it.
Example:
Get-Service | Get-Member
This will fetch all services and then get all members of the object returned from the Get-Service command.
The result might look scary if you are not used to it, but worth noting is that the first column is the name of the object member, and the next is the type.
A member of type Property means it can contain data of some form, while a member of type Method, means that there are ‘commands’ within the object that let you do stuff. If we take the above example, you will for instance find a Property called DisplayName. This property contains the display name for all the services. You will also find a method called Stop. This method lets you stop a service.
There you have it! By memorizing three simple commands, you have the power to find all commands available in PowerShell, look up how they work, and investigate the object they return to list all properties and methods they contain.
NOTE! If you really have a hard time remembering three commands in the beginning, you could get by with only knowing Get-Help and Get-Member. You see, Get-Help also supports wildcards. This means that if it can’t get an exact match, it will list all the commands matching the wildcard pattern.
Example:
Get-Help Get*
This will list all the commands starting with Get. The output is pretty much the same as using Get-Command.
Suggested next steps
As soon as you memorize these commands, and know how to use them, the next important PowerShell concepts that you should read up on are the Pipeline and Variables. Also knowing this will get you a long way in using PowerShell to help make your job easier.