Powershell Basics

 

Follow the free online training: Getting Started with PowerShell 3.0 Jump Start. The following information is from this training.

Some tips in advance:

  • Install the latest version of Powershell (PS)
  • Run PS as Administrator to have the appropriate rights
  • Update the PS help files on regular basis

Get-Help

Displays help on the screen.

  • When there are brackets [ ] after a string in the Syntax, you can fill in multiple items separated by a comma.
    • Get-service [[-Name] <String[]>]

  • When there are [ ] outside a complete parameter makes it parameter optional
    • Get-service [[-Name] <String[]>]

  • When there are [ ] outside a parameter makes it parameter positional
    • Get-service [[-Name] <String[]>]

  • Information about about stuff, worth reading..

 

The pipeline

Separates commands en puts the output of the first command to the next.

(Passtru displays the result.)

Filtering in the pipeline

Use a filter as far left in the pipeline for best performance. The pipeline processes in order of entering. In the first line the whole stuff is first sorted and then filtered. In the second line the stuff is first filtered and then sorted witch is obviously better performing.

Using $_ is used for the current object passing the pipeline:

In PS version 3 the new way of writing this is:

the status is here a property of get-service.

ByValue / ByPropertyName

First try ByValue. If that fails, try ByProperty. At third, create your own property. And final you can use Get-WMIObject

In the following code all services will be stopped. This is not a good idea but follow me for now.

In this case Get-service is the sending command and Stop-Service the receiving. To find out what type of object is being send across the pipeline you can get-member this command. In this case it is a Servicecontroller object:

Now lets see what the receiving commandlet is capable of receiving. You can view this in the helpfile of the receiving commandlet.

Look here for a InputObject parameter that you want to use and check if it is also a ServiceController[] object and has the value: Accept pipeline input?  true

ByPropertyname (as a string) can match on name <String[]> if the propertyname matches. So, the above command can actually stop some services.

Add a PropertyName in order to get a match trough the pipeline.

Take an existing property and create a different property with a new name.

  • @name = the name how you want to name the new property
  • Expression = What do you want to fill it with. The value name in this case.

If no Pipeline Input is provided for the commandlet

Get-WMIObject for instance: (btw: replace Get-WMIObject with Get-CimInstance witch supports pipeline input)

What ever is between ( ) is processed first. This will give the list of computernames. They are however the wrong “TypeName ADcomputer” (check with Get-Member). The ExpandProperty changes it to the “TypeName String” witch can be used as input for get-WMIObject.

Do this as a last resort as you want to use the power of the pipeline.

Admin tools

The results from PS commands are objects and not just text. As they are objects you can treat them as such and use them to do stuff to it. This extends even to imported text files for example.

The right words: Things (objects) have items (properties) and can do things (methods) like: A bike (object) has a steer (property) and you can peddle (method) it.

use them like:

The processes are the objects. The handles are properties and you can steer this information with where and sort.

Get-member shows you a bunch of stuff like:

  • the type of object like servicecontroller
  • Notice the properties and methods
    • Properties can be used in sort and select etc..

getmember

Leave a Reply

Your email address will not be published. Required fields are marked *