Top 5 Commands in PowerShell
Which commands are you always going to need to use, and will use with every new command or module as you write PowerShell?
You use this to read the help file for a cmdlet: Get-help <cmd>. So you see a script that uses Test-connection, and you want to see what you can do with it: get-help test-connection. If you want to see it in a separate window, in PowerShell v3 and higher: Get-help Test-connection –showwindow.
This opens full help in a separate window. You can limit what you see to just examples by changing the settings (upper right hand corner of the window). Get-help has other parameters: -examples, -parameters, -full or –detailed, which result in help being shown to you in your PowerShell session. Online help (use the –online parameter) will open a browser window to the proper MSDN help page.
2) Get-Help about*
This is your tool for learning more about PowerShell concepts and constructs. You can read the help files about_Command_Syntax, which tells you how to read help files. Or about*operator* which shows you there are about topics for type operators, comparison operators, and others. One of the more inspirational help files in v3 is about_workflows.
Get-command allows you to find commands, using wildcards, or using the –verb and –noun parameters. You could run: get-command –verb get –noun *serv*. Or you could start investigating all the commands in a module you haven’t used before: get-command –module smbshare.
This command will tell you in a tabular layout the name of the command, the command type: function, alias, cmdlet, and the module it comes from. You can also use get-help *servi* to see commands about services; this shows you the command name, the type, and a synopsis (the short description of what it does).
You pipe the output of a command to get-member to find out what properties you might want to use in a report. Get-member also shows you the .NET object type name, the methods, and events for that type of object. You can use any property in a select-object or format command following your initial command.
For example, get-service | get-member shows you the properties, events, and methods of a System.ServiceProcesss.ServiceController object, including DependentServices, DisplayName and MachineName. You could decide to run the following: get-service –computername lon-dc1 |select machinename, displayname, dependentservices.
5) Format-list -properties *
If you see a long list of properties, are the values in a format you want to use? Or are they in a format you will need to convert… like ticks instead of datetime? For example, you want logon information about users: You run:
get-aduser -filter * -prop * | select-object -first 1 | format-list -property *
OR Using shortened parameters:
get-aduser -filter * -prop * | select -first 1 | format-list *
You can see that there are lastlogon times that are datetime format, and some that are in ticks. You can decide to select only the properties that are in datetime format.
In this blog, you’ve seen the 5 commands you will use again and again no matter how experienced you get with PowerShell. These commands allow you to learn more, on your own, about PowerShell and the information you retrieve.