This lab is designed to help you become familiar with creating and exploring objects.
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.
"Some string object"
get-date
"string 1","string 2","string 3"
5,6,7,8,9
"string 1",456,(get-date),"string 2"
These tasks practice redirecting object display output to either save it or discard it instead of letting it show up on the screen.
mkdir $home/COMP2101/ps-lab2
get-date >$home/COMP2101/ps-lab2/mydate.txt
"chicken","tuna","rodent" >$home/COMP2101/ps-lab2/food
new-item -itemtype directory $home/COMP2101/ps-lab2/mydir1
new-item -itemtype directory $home/COMP2101/ps-lab2/mydir2
new-item -itemtype directory $home/COMP2101/ps-lab2/mydir3 | out-null
These tasks will demonstrate how to create and use aliases in Powershell.
get-alias
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
new-item -path alias:np -value notepad
get-alias
get-alias np
np
These tasks will demonstrate how to create and use functions in Powershell.
ls function:
mkdir $home/COMP2101/ps-lab2/mydir4
help
command. In particular, what is the last line of the script block in the help
function?
get-content function:help
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
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.
Pipelines are extremely common and useful in Powershell. These tasks will give you practice building and using them.
ls function: | more
get-alias | more
get-process
get-process | sort cpu
get-process | sort cpu |more
get-process | sort cpu | select -last 10
get-process | select -last 10 | sort cpu
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.
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
get-host | get-member
get-host | get-member -membertype method
get-host | get-member -membertype property
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
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
ps | get-member | more
get-wmiobject -class win32_process | get-member | more
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
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 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.
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.
get-mydisks
. Save your function to your profile file, so that your function can be used in any of your powershell windows.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.