Showing posts with label Begin PowerShell. Show all posts
Showing posts with label Begin PowerShell. Show all posts

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.......

Sunday, 4 January 2015

First Time PowerShell user?

First Time PowerShell user?

    I hear a lot and infact felt isolated when I've launched the PowerShell console for the first time and I think the unix console windows might have scared us to core to exhibit this common behaviour for windows guys or we are GUI stuck! :)
 
How to overcome the First Time issues?
    Step 1
         Just launch it believing its the extended version of Windows Command Prompt with a different color theme and I would like to highlight this "PowerShell supports all native windows commands".
When you launch for the first time.... try the dir, cd , date, time, ping etc it works!

   Step 2.
        Pin the PowerShell icon to the 'taskbar' on your system and start working with it by slowly replacing the 'cmd.exe' to PowerShell and start with the basic commands in PowerShell.

   Step 3.
        This would allow you to explore the new powerful console and what it could offer you on the long run and avoid cmd.exe as much as possible.

Bottom line for this simple strategy is to allow you to get more familiar with PowerShell, and yay! it looks cool.

Some DOS to PS basics for you to try ..

Change a Directory
  • DOS: cd
  • PowerShell: Set-Location
List Files in a Directory
  • DOS: dir
  • PowerShell: Get-ChildItem
Rename a File:
  • DOS: rename
  • PowerShell: Rename-Item 
Find Date:

  • DOS : date
  • PowerShell: Get-Date

We would certainly get into depth of each command, but I would recommend you to observer the PowerShell commandlets carefully.

PowerShell commandlets follow a Verb-Noun format. Example (get-date : get-verb, date-noun)

Give it a try: Guess what would be the PowerShell commandlet for checking all the services on a system?

If your answer is (Get-Service) .. way to go! if not, still long way to go :)

Procedure to Pin PowerShell to Taskbar and How to launch PowerShell console.

Its important to launch PowerShell with "Elevated Privileges" on a system to avoid any issues while executing the commandlets.

Right Click on Windows PowerShell and select "Pin to Taskbar"







Then Right Click on the PowerShell Icon and Select "Run as Administrator". Just remember this should be the standard procedure of launching PowerShell to avoid any errors related to permissions.

















The PowerShell Console should always have "Administrator" on the Titlebar just to be sure you are Administrator and powershell is running with Administrator privileges.