Objects, Properties and Methods Part 2

Sign up to access this lesson

Click here to sign up and get access to this lesson!

Saving Progress...

Let’s take a look at some examples using Methods:

We’re going to be using get-process and the process named notepad

  • First let’s open notepad, open your search bar and press notepad, minimize to the taskbar
  • Type get-process, press return, press return again. Scroll up, and we see that notepad is running. Press return and cls.
  • Type get-process -name notepad press return, now press return again

In our boat and car illustration we showed you that objects contain properties and methods, but when we take a look at get-process there are no methods or properties listed.

So, the question is, how can I display the property or methods for get-process?

  • By using a special command called get-member. We’ll use the alias gm for get-member.
  • Type get-process -name notepad | gm. (Use pipe operator) That’s the symbol right above the enter key. Press your shift key and your pipe operator.

If you recall, a pipeline takes the output of one command and pushes it through to the input of the second command. 

So, get-process has a process called notepad, and you’re piping the output of notepad into the input of get-member. Press return

  • There is some very important information, when using get-member. 

It’s TypeName. Typename tells us what kind of object that is being sent across the pipeline. In this case the TypeName is System.Diagnostics.Process but we’ll just use the last part which is process. Just keep that in mind, you’ll need it later when we get to the pipeline lecture.

  • Notice the method called kill. Kill is definitely an action.

Go ahead and type this out then we’ll explain it

  • Type (Get-Process Notepad).kill() 
  • To use a method of an object, Place the cmdlet name and the argument in parenthesis, then type a dot (.), then the method name, and a set of parentheses "()"

The parentheses are required for every method call, even when there are no arguments.

The point is that we don’t need a separate cmdlet to close the process notepad we have the method kill. Press return

And we see that notepad has been closed.

Let’s try another example of using Methods

  • Let’s say we want to copy files from one location to another. In this case we’ll use the get-childitem command.
  • Let open windows explorer, for demonstration purposes I’ve created a folder named content

Double click on content and we have a text file named computers. click that file and displayed is the contents of the text file. 

  • Now we want to copy this file from the C:\content folder to the c:\test folder using a method called copyto.

Type get-childitem | GM – (Pipe operator) press return

Notice the get-childitem TypeName: is FileInfo 

We’ll use the copyto method to copy a file from one location to another

  • We don’t need a cmdlet, not when we have the copyto method, press return

Let’s go ahead and type the command then I’ll explain it.

Type (Get-childitem C:\content\computers.txt). copyto("C:\test\computers.txt") 

  • Put the cmdlet name and the argument in parenthesis then type a dot (.), the method name, in this case is copyto. and a set of parentheses "()"

Sign up to access the rest of this lesson

You must either log in or sign up to access this lesson.

Saving Progress...

0 0 votes
Lesson Rating
Subscribe
Notify of
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
profile avatar
Ambrose Garza(@ambroseg)
Member
Points: 4122
3 months ago

Every time I ran the get-childitem | gm command it would tell me the type was directory info and not file info. I was running it from c:\users\administrator directory when I switched to c:\windows\system32 it showed the type as both directory info and file info.

Last edited 3 months ago by Ambrose Garza
profile avatar
Ricardo P(@ricardop)
Admin
Power Student
Points: 17376
Reply to  Ambrose Garza
3 months ago

Hi profile avatar Ambrose Garza
That’s correct, same behavior from my end.
The difference is in what the folder contains.
Ricardo

Administration and Automation with Windows PowerShell

0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 42min

0 / 7 lessons complete

Getting Help and Finding Commands

• 39min

0 / 6 lessons complete

PowerShell Command Syntax

• 33min

0 / 5 lessons complete

PowerShell Objects and Properties

• 35min

0 / 6 lessons complete

The PowerShell Pipeline

• 24min

0 / 2 lessons complete

PowerShell Providers

• 30min

0 / 5 lessons complete

PowerShell Arrays and Variables

• 28min

0 / 4 lessons complete

PowerShell Loops

• 19min

0 / 3 lessons complete

PowerShell Conditional Statements

• 11min

0 / 1 lessons complete

On Premises Lab Setup

• 36min

0 / 8 lessons complete

Basic Domain Administration with Windows PowerShell

• 2hr 27min

0 / 10 lessons complete

Send Emails with PowerShell

• 22min

0 / 2 lessons complete

PowerShell Desired State Configuration (DSC) Basics

• 1hr 48min

0 / 6 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete