COMP2101

Lab 2 - Powershell Objects

This lab is designed to help you become familiar with creating and exploring objects.

Object Display

These tasks will help you to become familiar with how Powershell by default displays any objects that are not intentionally sent anywhere on the command line.

  1. Create a simple string object and let it be automatically displayed
    "Some string object"
    
  2. Create an object using a cmdlet and let it be automatically displayed
    get-date
    
  3. Create some object collections and let them be automatically displayed
    "string 1","string 2","string 3"
    5,6,7,8,9
    "string 1",456,(get-date),"string 2"
    

Output Redirection

These tasks practice redirecting object display output to either save it or discard it instead of letting it show up on the screen.

  1. Use shell output redirect to send the display output of a cmdlet to a file in a direcotry created to hold files created during this lab.
    mkdir $home/COMP2101/ps-lab2
    get-date >$home/COMP2101/ps-lab2/mydate.txt
    
  2. Use shell output redirection to send the display output of an object collection to a file.
    "chicken","tuna","rodent" >$home/COMP2101/ps-lab2/food
    
  3. Create some directories on your system and notice how the cmdlet that makes a directory does not discard the directory object it creates.
    new-item -itemtype directory $home/COMP2101/ps-lab2/mydir1
    new-item -itemtype directory $home/COMP2101/ps-lab2/mydir2
    
  4. Create a directory and discard the output it made instead of letting it display.
    new-item -itemtype directory $home/COMP2101/ps-lab2/mydir3 | out-null
    
  5. Use Windows Explorer to review the files and directories you have created.

Aliases

These tasks will demonstrate how to create and use aliases in Powershell.

  1. Run the get-alias cmdlet to display the pre-defined aliases in Powershell.
    get-alias
    
  2. Review the list of pre-defined aliases. How many Linux commands do you see in the list?
  3. The Linux ls command is an alias in the list. Try listing a directory using the ls function. Does it work the same as the Linux version?
    ls $home/COMP2101/ps-lab2
    
  4. Try creating your own alias for notepad. Review the alias list to see if it shows up in the list. Try looking up just the alias you created by name. Try using it to start notepad. Quit the notepad window when it starts.
    new-item -path alias:np -value notepad
    get-alias
    get-alias np
    np
    
  5. Start another Powershell window. In the new window, display the list of aliases. Is your alias in the list? What does that tell you about aliases that you create?
  6. Add a line to your powershell profile that creates the notepad alias.
  7. Start a new powershell window and run the np command.

Functions

These tasks will demonstrate how to create and use functions in Powershell.

  1. Display a listing of the pre-defined functions in Powershell.
    ls function:
    
  2. Review the list of pre-defined functions. How many Linux commands do you see in the list?
  3. The Linux mkdir command is a function in the list. Try creating a directory using the mkdir function. Does it work the same as the Linux version?
    mkdir $home/COMP2101/ps-lab2/mydir4
    
  4. Try viewing the script block inside the pre-defined function which implements the help command. In particular, what is the last line of the script block in the help function?
    get-content function:help
    
  5. Create your own function. See if it shows up in the functions listing. Try viewing just your function on the functions drive and then display the script block content of your function. Use your function as a command.
    function myfunc {
    "this is my function"
    "it creates a couple of string objects and just lets them be displayed by default"
    }
    ls function:
    ls function:myfunc
    get-content function:myfunc
    myfunc
    
  6. Start another Powershell window. In the new window, display the list of functions. Is your function in the list? What does that tell you about functions that you create?
  7. Modify your profile to create and run a function which displays a welcome message like the one from the bash labs. You can get sample code for displaying the welcome message from welcome-profile.ps1. Name your function welcome.

Out verb cmdlets

The out verb is used for several cmdlets which can be helpful in saving or displaying or destroying objects at the end of a pipeline.

  1. Try each of the commands on the “Out Verb Cmdlets Exercises” slide from the presentation.

Pipelines

Pipelines are extremely common and useful in Powershell. These tasks will give you practice building and using them.

  1. Use a pipeline to view long output one screenful at a time.
    ls function: | more
    get-alias | more
    
  2. Use a pipeline to modify the ordering of objects passing through the pipeline, and then display the output one screenful at a time.
    get-process
    get-process | sort cpu
    get-process | sort cpu |more
    
  3. Objects pass through the pipeline from left to right. The ordering of commands in a pipeline is very important. What happens if you change the order of commands in a pipeline? Can you explain any differences in output between these two pipelines?
    get-process | sort cpu | select -last 10
    get-process | select -last 10 | sort cpu
    

Object Exploration

We can explore objects using Powershell. It can be very useful to know the structure of an object as well as to see what can be found in the properties of objects we create.

  1. Create a host object. This object contains information and code related to our current Powershell process. When we use the get-host cmdlet to create this object and don’t do anything with that object, it gets displayed in the default format for that object type, which is what happens to objects which “fall off the end of the command line”.
    get-host
    
  2. Pipe the host object to the get-member cmdlet to see what that object contains that we can see or use. The list may be onger than you expected. Try viewing just the methods of the object. Try viewing just the properties of the object.
    get-host | get-member
    get-host | get-member -membertype method
    get-host | get-member -membertype property
    
  3. Try the same exercise with the object from the get-date cmdlet. Note that the default display format of this object type is quite different than the display format of the host object. The datetime object has multiple types of properties, so try viewing both the simple properties list and the complete properties list.
    get-date
    get-date | get-member
    get-date | get-member -membertype method
    get-date | get-member -membertype property
    
  4. For both the previous objects, you should see that there are quite a few data properties in the objects which are not displayed by the default object display format. More complex objects do this to a much greater degree. Try viewing process objects produced by get-process, ps, and get-wmiobject -class win32_process to see what the default outputs look like.
    get-process
    ps
    get-wmiobject -class win32_process
    
  5. Try viewing the object member for each of these to see what they look like. Do both commands produce the same types of objects?
    ps | get-member | more
    get-wmiobject -class win32_process | get-member | more
    
  6. Get-member is very useful to find out what is supposed to be in an object for us to access and use. However, not all object properties have data or get used. To view the data saved in the properties of an object, we can use the format-list cmdlet and simply tell it to show us all properties. Try this with some of the objects we have been using so far to see which properties have data in them and which are empty or unused.
    get-host | format-list *
    get-date | format-list *
    get-process | format-list * | more
    get-wmiobject -class win32_process | format-list * | more
    
  7. Create a function to display your cpu information (use get-ciminstance cim_processor to get the object to use for this). Display it as a list of properties, and only include the CPU manufacturer and model, current and maximum speed, and number of cores for each processor in your system. Add the function to your profile so that it is always available similar to the welcome function. Name this function get-cpuinfo.

Dot Notation

Dot notation allows us to access the members of an object. We can retrieve or set the properties of an object, and we can invoke methods in an object to make it perform tasks for us.

  1. Try each of the commands on the “Dot Notation Exercises” slide from the presentation.

Custom Objects

Custom objects provide a way to make objects that contain whatever data we find useful. It’s kind of like being able to have variables with super powers. Custom objects can be built on top of any existing object types.

  1. Try the examples on the “Custom Objects Examples” slide from the presentation.
  2. Create a new function that shows you specific disk information. It should display a table of your disk drives, only showing Manufacturer, Model, SerialNumber, FirmwareRevision, and Size. Name your function get-mydisks. Save your function to your profile file, so that your function can be used in any of your powershell windows.

Grading

This lab prepares you for the next lab and expands your scripting skills in preparation for lab 5 which is the only powershell lab that is marked.