Sunday, 15 July 2012

Basic PowerShell Commands


·       Get-Help


The first PowerShell cmdlet should learn is Get-Help. You can use this command to get help with any other command.
For example, if you want to know how the Get-Process command works, you can type:
Get-Help -Name Get-Process
And Windows will display the full command syntax.
You can also use Get-Help with individual nouns and verbs.
For example, to find out all the commands you can use with the Get verb, type:

Get-Help -Name Get-*

·       Get-Service

The Get-Service command provides a list of all of the services that are installed on the system. If you are interested in a specific service you can append the -Name switch and the name of the service (wildcards are permitted) When you do, Windows will show you the service’s state.

·       ConvertTo-HTML

PowerShell can provide a wealth of information about the system, but sometimes you need to do more than just view the information onscreen. Sometimes, it’s helpful to create a report you can send to someone. One way of accomplishing this is by using the ConvertTo-HTML command.
To use this command, simply pipe the output from another command into the ConvertTo-HTML command. You will have to use the -Property switch to control which output properties are included in the HTML file and you will have to provide a filename.
You want to create an HTML report that lists the name of each service along with its status (regardless of whether the service is running). To do so, you could use the following command:
Get-Service | ConvertTo-HTML -Property Name, Status > C:\services.htm

·       Export-CSV

Just as you can create an HTML report based on PowerShell data, you can also export data from PowerShell into a CSV file that you can open using Microsoft Excel. The syntax is similar to that of converting a command’s output to HTML. At a minimum, you must provide an output filename. For example, to export the list of system services to a CSV file, you could use the following command:
Get-Service | Export-CSV c:\services.csv

·       Select-Object

If you tried using the command above, you know that there were numerous properties included in the CSV file. It’s often helpful to narrow things down by including only the properties you are really interested in. This is where the Select-Object command comes into play. 
The Select-Object command allows to specify specific properties for inclusion. For example, to create a CSV file containing the name of each system service and its status, you could use the following command:
Get-Service | Select-Object Name, Status | Export-CSV c:\services.csv

·       Get-Process

Just as you can use the Get-Service command to display a list of all of the system services, you can use the Get-Process command to display a list of all of the processes that are currently running on the system.

·       Stop-Process

Sometimes, a process will HANG. When this happens, you can use the Get-Process command to get the name or the process ID for the process that has stopped responding. You can then terminate the process by using the Stop-Process command. Terminate a process based on its name or on its process ID. For example, Terminate WINWORD by using one of the following commands:
Stop-Process -Name winword
Stop-Process -ID xxxx
Keep in mind that the process ID may change from session to session.
Cheers :)

No comments:

Post a Comment