PowerShell Objects


This entry is part 8 of 9 in the series PowerShell

What are PowerShell objects? Before discussing what they are, we’ll go back in time to the world of the command line. We have an old post on the DOS Command Line which you should have a look at if you are not familiar with the command line.

Command Line

  • There are internal (built-in) and external commands.
  • You can create batch files, which is great.
  • Has pipelining, which is great, but it is limited.
  • It’s limited because the output of a command that’s sent to the next command is just a text string.

PowerShell

  • PowerShell does not convert output to text.
  • PowerShell converts output to objects.

A good YouTube video on this topic is PowerShell Master Class – PowerShell Fundamentals. At the time 8:05 John Savill begins to talk about the advantages of PowerShell over the command line. He uses the example of the dir command. If you run Get-Alias dir you will see that it is Get-ChildItem. Note that gci is also Get-ChildItem. Also note that ls is also Get-ChildItem. ls is a command in Linux for “list”. To test the returning of objects, try these commands in your own directory.

dir | sort-object
dir | sort-object -Descending
dir | sort-object -Descending -Property LastWriteTime
dir | foreach {"$($_.GetType().fullname) - $_"}

Here are some more to try. -path lists files and folders only in the top level directory. -recurse drills down to include all of the subfolders as well. gci is Get-ChildItem.

gci -path "C:\temp\"
gci -recurse "C:\temp\"

Files

I have a simple text file with a full name of C:\some.txt. In PowerShell, if I run gci “some.txt” in the C:\temp\ directory I will get the directory and some information about the file, anmely Mode, LastWriteTime, Length and Name. There is so much more. If you run gci “some.txt” | get-member You will see a list of methods, properties and other things in three columns called Name, MemberType and Definition. We can use these in a select statement. Here is our example.

PS C:\temp> gci "some.txt" | select Name, FullName, Extension

Name     FullName         Extension
----     --------         ---------
some.txt C:\temp\some.txt .txt

Processes

Let’s start and stop a program called Notepad using only PowerShell. All you need to do is type notepad and press Enter and after a few seconds the program will start. To see a list of all processes running, just type get-process. The list will likely have dozens of processes. If you want all of the processes starting with the letter n, type get-process n*. Run the following command: get-process | where-object {$_.Name -eq “notepad”}. If you replace Name with ProcessName you will get the same results. Just as with files, you can also tack | get-member onto the end of that last command. You could use the alias gm instead of Get-Member. Below is just the command to run the program and stop it.

PS C:\temp> notepad
PS C:\temp> get-process | where-object {$_.ProcessName -eq "notepad"} | stop-process
PS C:\temp>

Let’s use the Kill method to close Notepad. We’ll need to use the two brackets to actually call the method.

PS C:\temp> notepad
PS C:\temp> (get-process | where-object {$_.ProcessName -eq "notepad"}).kill

OverloadDefinitions
-------------------
void Kill()



PS C:\temp> (get-process | where-object {$_.ProcessName -eq "notepad"}).kill()
PS C:\temp>

If you have multiple instances (oe even just one) of a process and you want to get the process list, perhaps of Notepad, the best way is to do it this way. note that we specify notepad early on (left side) in the command to reduce the number of objects that travel down the pipeline. We only passed 2 objects to the sort-object command.

PS C:\temp> notepad
PS C:\temp> notepad
PS C:\temp> get-process -name notepad | sort-object -property id

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    240      13     2976      14492       0.08   1536   1 notepad
    236      13     2916      14252       0.16  21932   1 notepad


PS C:\temp>

We can stop all 2 of them this way.

PS C:\temp> get-process -name notepad | sort-object -property id | stop-process
PS C:\temp>

Objects

PowerShell does not convert output to text, but maintains them as objects, which is great for us to work with. We still have all of the power available in things like methods and properties. This PowerShell feature enables chaining of commands. To chain commands, we can use the pipeline. We have more information on the pipeline in the post called PowerShell Pipeline Output.

Suppose I had Notepad running. Let’s run get-process. Let’s filter that to just look at the Notepad process. What do we get in return? We get the object, not just text. To prove that, run the following commands. Here is the partial screenshot od a session. Click the picture to see a larger image.

TypeName: System.Diagnostics.Process.

Since PowerShell keeps the object and does not merely display some text on the screen, we can manipulate that object. One thing we could do is stop the process. At the end of the command we could use | stop-process. Another way to do this is to use the Kill() method of the notepad process object. We need to use the brackets, just like in math class when we use brackets to make sure something is evaluated first.

(get-process | where-object {$_.name -eq "notepad"}).kill()

Let’s be more efficient and save some computer computation with the code below. This will kill all instances of Notepad. Again, notice the use of brackets.

(get-process -name notepad).kill()

Variables

A variable can contain an object and we can use these variables down the pipeline.

Series Navigation<< PowerShell CommentingPowerShell Manipulating Objects >>