Saturday 17 January 2015

6 Powershell Cmdlets to start with.

6 Powershell Commands to start with.



      Powershell is the new management tool of the Microsoft, they have been promoting PS in every aspect. With Powershell there are a lot many managements tasks that could be achieved much quicker and ability to work in much detailed manner which the GUI fails to provide. As a systems administrator we would have to be familiar with some important yet basic commands to get started with. Included is some of the one liners that would certainly speed up our day to day activities and ease server management activities.

I would like to start off with the basic commands the moment we login to server to check something..

1.Check service status. (Get-Service)

As a recommendation from my end. Launch Powershell as Administrator and start typing the commands as we see in this page.

Lists all the services.( observer the output, you would see Status, Name and DisplayName)

PS C:\>get-service



Find service with Name ( observer the output, you would see Status, Name and DisplayName), Here we are querying with Name )

PS C:\>get-service -name bits



Find service with Name ( observer the output, you would see Status, Name and DisplayName), Here we are querying with DisplayName.Using wildcards "*" for search)




2.Check Process status. (Get-Process)

This command is used to check the running processes on the system.

Lists all the processes running on the system
PS C:\get-process 

 

Find process with Name ( observer the output, you would see Status, Name and DisplayName), Here we are querying with Name )

PS C:\> get-process -name notepad


Sort process with highest utilization

PS C:\>Get-Process | Sort-Object WS -Descending



3. Get-Command

         The Get-Command is one of the most useful cmdlets in the whole of PowerShell, as it will help you getting to know PowerShell by letting you search for certain cmdlets. Using Get-Command alone would provide all the commands, so we have to ensure we provide some parameters for the cmdlet to search.


To get the cmdlets

PS C:\> get-command -CommandType cmdlet


4. Get-Help

          Once you have found the cmdlet you are looking for using Get-Command, you are going to want to know the syntax and how you can use that specific cmdlet. This is where Get-Help will do the rest.
Get-Help give indepth details of each cmdlet, its usage and syntax. In addition it also provides examples when '-examples' switch is used with it.


If you need help with examples for any command we need to add the switch -examples to the command.


 5.Start and Stop Service

       To stop service, issue cmdlet stop-service followed by the service name. 


         To start service, issue cmdlet start-service followed by the service name.


Note: get-service, stop-service and start-service accepts wild cards for searching the name of the service. So Don't try stop-service -name *. It would case new issues then we already have :) .


6.Stop-Process.

        Sometimes, a process will freeze up. 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. You can terminate a process based on its name or on its process ID. For example, you could terminate Notepad by using one of the following commands:

PS C:\>Stop-Process -Name notepad
PS C:\>Stop-Process -ID 6088



  • Also with the above examples, we could get familiar with the type of errors we get in PowerShell.
  • We opened a notepad in step 1, checked its details in step 2, killed it using -name parameter in step 3.
  • We opened a notepad in step 4, checked its details in step 5, killed it using -ID parameter in step 6.

7.Set-ExecutionPolicy

Microsoft has disabled scripting by default in an effort to prevent malicious code from executing in a PowerShell environment.
You can use the Set-ExecutionPolicy command to control the security surrounding PowerShell scripts.
The options available:

  • Restricted — Restricted is the default execution policy and locks PowerShell down so that commands can be entered only interactively. PowerShell scripts are not allowed to run.
  • All Signed — If the execution policy is set to All Signed then scripts will be allowed to run, but only if they are signed by a trusted publisher.
  • Remote Signed — If the execution policy is set to Remote Signed, any PowerShell scripts that have been locally created will be allowed to run. Scripts created remotely are allowed to run only if they are signed by a trusted publisher.
  • Unrestricted — As the name implies, Unrestricted removes all restrictions from the execution policy.

Adding more.......

1 comment: