In this post, I will cover the seven most frequently used PowerShell commands that are useful for beginners. These are the commands you can start playing with during your PowerShell learning exercise.
PowerShell provides a way to get help about any command that you want by using the Get-Help cmdlet. Using this cmdlet, you can check the syntax, usage examples, and detailed description of PowerShell cmdlet parameters and function. The beauty of this cmdlet is, if you don’t know what commands are available for managing services, just type Get-Help Services to display all commands that match the services criteria.
PS C:\> Get-Help Service
You can pick any cmdlet from the output of the above command to get help about it. For example:
PS C:\> Get-Help -Name Get-Service
Reading the contents of a file is the most frequent requirement for beginners who are trying to learn PowerShell. Reading files is kind of tough for system administrators who try to code this task in VB or Perl because they need to explicitly open a file handle and then read line by line. With PowerShell, it is easy. A layman can read the contents of a file just by passing it to the Get-Content cmdlet. Look at the example below for more details.
PS C:\> Get-Content C:\scripts\Computers.txt
If you need more detailed help about this cmdlet, try the Get-Help command:
PS C:\> Get-Help Get-Content -Detailed
This cmdlet lists all the services installed on a Windows computer. You can use this cmdlet to query a single service, multiple services, or all services on a computer.
PS C:\> Get-Service wwansvc, spooler
Status Name DisplayName
------ ---- -----------
Running spooler Print Spooler
Stopped wwansvc WWAN AutoConfig
Stop-Service and Start-Service
Stopping and starting services is the most common thing we Windows administrators do at work. PowerShell has built-in cmdlets that do this job easily without opening any MMCs. Using these cmdlets, you can stop/start services on either local or remote computers. The beauty of these cmdlets is that they take care of starting/stopping dependency services and wait for services to start/stop. Below are a few examples.
Start/Stop service on local computer:
PS C:\> Stop-Service -Name Spooler
PS C:\> Start-Service -Name Spooler
Start/Stop service on remote computer:
PS C:\> $ServiceObj = Get-Service -ComputerName MyPC1 -Name spooler
PS C:\> Stop-Service -InputObj $ServiceObj
PS C:\> Start-Service -InputObj $ServiceObj
You may also want to check one of my previous posts, where I talk about restarting a service on multiple remote computers using PowerShell. This post includes a sophisticated script to restart services across your network.
This cmdlet comes in very handy for knowing which processes are running on local or remote computers. Along with the process name and process ID (PID), the cmdlet gives other useful information such as the executable path of the process, company name, executable version number, and memory utilization of the process. Following are examples of using the command to get complete details of the running processes.
Get details about running processes on local computer:
PS C:\> Get-Process | Format-List * -Force
Get details about running processes on remote computer:
PS C:\> Get-Process -ComputerName MYPC1 | Format-List * -Force
This cmdlet helps you stop a process on a local or remote computer. It takes the process name or PID and kills that process. This is very useful in scenarios where an application is hung and is not responding.
Stop a process with ID 22608 on local computer:
PS C:\> Stop-Process -Id 22608
Stop all Excel processes on local computer:
PS C:\> Stop-Process -name excel
Tip: Though Stop-Process does not have the -ComputerName parameter, you can still use it for killing remote processes by using the trick below:
PS C:\> $Obj = Get-Process -Name excel -ComputerName MYPC1
PS C:\> Stop-Process -InputObject $Obj
You may want to refer to my previous article about querying and killing a process on a remote computer using PowerShell and WMI. That article gives you alternate ways of managing processes, as well as a sophisticated script to query and stop processes.