PowerShell Key Commands

17
PowerShell Key Commands Table of Contents PowerShell Key Commands ............................................................................................................ 2 Get-Command ................................................................................................................................. 3 Get-Help .......................................................................................................................................... 4 PowerShell Aliases -1 ...................................................................................................................... 6 PowerShell Aliases -2 ...................................................................................................................... 7 Get-Service ...................................................................................................................................... 8 WhatIf ............................................................................................................................................. 9 Confirm ......................................................................................................................................... 11 Stop a Running Service ................................................................................................................. 12 Start a Stopped Service ................................................................................................................. 14 Piping and the Pipeline ................................................................................................................. 15 Piping Example .............................................................................................................................. 16 Notices .......................................................................................................................................... 17 Page 1 of 17

Transcript of PowerShell Key Commands

Page 1: PowerShell Key Commands

PowerShell Key Commands

Table of Contents

PowerShell Key Commands ............................................................................................................ 2

Get-Command ................................................................................................................................. 3

Get-Help .......................................................................................................................................... 4

PowerShell Aliases -1 ...................................................................................................................... 6

PowerShell Aliases -2 ...................................................................................................................... 7

Get-Service ...................................................................................................................................... 8

WhatIf ............................................................................................................................................. 9

Confirm ......................................................................................................................................... 11

Stop a Running Service ................................................................................................................. 12

Start a Stopped Service ................................................................................................................. 14

Piping and the Pipeline ................................................................................................................. 15

Piping Example .............................................................................................................................. 16

Notices .......................................................................................................................................... 17

Page 1 of 17

Page 2: PowerShell Key Commands

PowerShell Key Commands

14

PowerShell Key Commands

**014 All right. Like anything else, probably one of your best friends in PowerShell is help. If I don't know what the next steps are, if I don't know what the commands are, or the cmdlets are, help is going to start me. So, if I want to memorize any cmdlets, probably Get-Help is the first one. The second one, I would probably memorize is Get-Command. As its name implies it gives me a list of all the cmdlets that are available to me. And then Get-Members shows objects, methods, and properties. We'll take a look at Get-Member as a possible-- as a helpful tool a little bit later on.

Page 2 of 17

Page 3: PowerShell Key Commands

Get-Command

15

Get-Command

Can be used to find cmdlets

Shows all commands with the verb “Get”

Just a snippet of the output on the right

**015 But Get-Help-- actually, this is the Get-Command command or cmdlet. In this particular case, the example says we want to know what commands out there start with the word get. So, to find that out, I say Get-Command. And then I say I'm looking for the verb get. And, by the way, they all have that same format. All of the cmdlets have that same format, verb noun format. So, I could also do Get-Command, and instead of verb, I can say noun and say history, and find out all the commands that have the word history in them-- or have the noun

Page 3 of 17

Page 4: PowerShell Key Commands

history in them, for example. So, Get- Command gives you a list of what's available for you. And, by the way, this is not all of the get cmdlets that are available. It's just what we could capture on the screen and still be able to see.

Get-Help

16

Get-Help

Can be used and learn more about

• Command usage• Command syntax

Is the same as

**016 Let's see. Help, I said that is our friend. I want to gather information about what the set command is. So, we type in Get-Help set. And it gives us-- you guys are probably familiar with this type of formatting. We've seen it. This is basically the way the help screen

Page 4 of 17

Page 5: PowerShell Key Commands

looks in the command line interface with CMD.com, for example. Gives us the name, gives us basic syntax that we'll want to use it in, and then a description of how it might be utilized. By the way, I don't have to type in Get-Help. I can just type in help, just like we did in CMD. One of the nice things that Microsoft did was a lot of the commands that we are familiar with from the command prompt, they are available to us through PowerShell. So, I can do a dir and get a directory listing. I can also make my own cmdlets if I want.

Page 5 of 17

Page 6: PowerShell Key Commands

PowerShell Aliases -1

17

PowerShell Aliases -1

Aliases are created for common commands.

Built-in PowerShell aliases - Microsoft has included many of the commands we have become familiar with.

• Cls – clear screen• Cp – copy• Del – delete• And more

**017 I can create some aliases to do a particular job. And so, when I think about aliases, some of them are already existing like clear screen. But if I wanted to create an alias for-- instead of doing a dir, I wanted to do an ls command, I could create an alias and do my own ls. So, here are a couple of the aliases that are pre- canned, pre-packaged for you, CLS, copy, delete, the typical ones we're familiar with.

Page 6 of 17

Page 7: PowerShell Key Commands

PowerShell Aliases -2

18

PowerShell Aliases -2

Custom aliases can be created as well.• The command below creates an alias “gs” for the Get-Service.

**018 Here is how I create my own alias. In this case we're creating one, just an alias called gs. And we're saying the gs alias is going to do get service. I like typing gs. One thing to note about the alias when it's done this way is it's non- persistent. So, it's only for this session. So, there is a way to create aliases that would be persistent. And you could share them across multiple machines if you wanted to, but the simple set alias command is a non- persistent command.

Page 7 of 17

Page 8: PowerShell Key Commands

Get-Service

19

Get-Service

Run the Get-Service cmdlet to see a list of services and their status.

We can see that the VMware DHCP service is running.

**019 So, in this particular case, we're trying to find out what services are running. So, we did a get service cmdlet and we can see on the left hand side, the services that are running, and we can see here is a service that is stopped. So, this at the top when the command first appears, when the output first appears, it would say this is the status. And we will look at ways to try to hone in on specific services that are of interest to us. Instead of doing a get service and seeing this whole scrolling list go by, we'll look at how can we-- how can we hone in and

Page 8 of 17

Page 9: PowerShell Key Commands

just see one or two things. So, in this case we're looking at VMware DHCP service is running.

WhatIf

20

WhatIf

The WhatIf parameter allows PowerShell users to test the command before actually executing.

Type Stop-Service –Name VMnetDHCP• If you are not sure, use the -WhatIf parameter.

**020 Another nice little tool that's available to us in PowerShell is WhatIf. We can type a cmdlet, and then tag on the dash what if argument to the end of it, and hit enter. And the command itself does not actually run. But it would tell us what would be the end result if it did run. So, in this particular case, we're pretending that we're going to stop the service VMnetDHCP. And so, the command we typed in is stop service,

Page 9 of 17

Page 10: PowerShell Key Commands

the name, VMnetDHCP, and then I said WhatIf. And here it tells me what the output would be. It would perform the operation of stop service on the target of VMware DHCP service. Now, one thing that people would like to see, and it does not do this, it does not show you the-- it does not tell you that if you stop this service, your DHCP service on your VM network is not going to work for you. It does not give us that kind of feedback. It just simply says if you type that command, this is what the output is going to be. The service will stop running. So, we have WhatIf. That's a nice little utility--

Page 10 of 17

Page 11: PowerShell Key Commands

Confirm

21

Confirm

The Confirm parameter allows PowerShell users to execute a command, but will ask “are you sure?”

This is especially helpful when a command does multiple actions.

Careful! The default selection is Yes

**021 Available to you. We also have the confirm argument. So, I can type in a command, and then I type in confirm. And then it will give me some options. It's just-- it's simply a way of saying are you sure you want to do this. So, if I'm about to do something that is maybe potentially dangerous, I might type in the command and then put in confirm, and then see what it's telling me. Are you sure you want to perform this action? And by performing this action, we said start a service, the one that we just stopped. By performing this action the VMware DHCP service is going to start up again. The default, by the way, is just hitting enter. And the yes is selected.

Page 11 of 17

Page 12: PowerShell Key Commands

If I'm doing multiple cmdlets together-- we'll talk about pipes and piping in a few moments. If I'm doing multiple cmdlets together, and I do the confirm at the end, I can just hit the A, and say yes to all, or no I don't want to do this, or no to all. And or-- let-- hold on a minute, let's pause. So, we have the suspend capability. All right, so confirm. Default selection is yes. So, be careful about that.

Stop a Running Service

22

Stop a Running Service

Type Stop-Service –Name VMnetDHCP

Get-Service verifies the service has been stopped.

Note: Must choose “Run As Administrator” when executing PowerShell.

**022 All right so, stopping a running service, one of the first things we want to do is find out what services are running. In order to do

Page 12 of 17

Page 13: PowerShell Key Commands

that, we do the get service command as we saw a few minutes ago. And the status tells us which ones are running. In order for me to stop a running service, that seems like a pretty administrative type of task, I should probably be-- have elevated privileges to do it. So, I have to open up PowerShell with the run-as command in order to do some of these things. So, stop service-name and then give it whatever name you want stopped. And then to confirm it has stopped, all I have to do is do the get service, remember? Get service, and I can say dash name again, and it would confirm that it has been stopped for me.

Page 13 of 17

Page 14: PowerShell Key Commands

Start a Stopped Service

23

Start a Stopped Service

Type Start-Service –Name VMnetDHCP

Get-Service verifies the service has been stopped.

Note: Must choose “Run As Administrator” when executing PowerShell.

**023 Starting the service again, Start-service, the name, and again, get service is the confirmation. And also, to start a service, I have to run it as administrator.

Page 14 of 17

Page 15: PowerShell Key Commands

Piping and the Pipeline

24

Piping and the Pipeline

Piping – passing output from one cmdlet into another cmdlet

Pipelining – stringing many commands together

“Pipelining uses piping”

**024 Piping, piping and pipelining, it is concatenating two or more commands together. When we think about piping, effectively, what we're doing is we run a cmdlet. And the output from that cmdlet is going to be fed in as input to the next cmdlet. So, that's the piping process. Pipelining is when I concatenate a bunch of these things together.

Page 15 of 17

Page 16: PowerShell Key Commands

Piping Example

25

Piping Example

Get-ChildItems c:\Windows | Where-Object {$_.Length –gt 200KB} | Sort-Object Length

The above pipeline retrieves objects in the Windows directory as long as those objects are greater than 200KB in size and the output is then sorted by size.

**025 So, an example would be this. It's kind of hard to see, so let me try and highlight it here. The command is Get-ChildItems. So, we're trying to get child items in the C:\Windows directory. So, that's the first cmdlet. The second thing, we're going to pipe it. That's what this little vertical line is. We're going to pipe that output to the where object is. And we're going to give it a specific parameter where the length is greater than two hundred kilobytes. And then we're going to out put that. We're going to pipe that output to-- we're going to sort the object by length.

Page 16 of 17

Page 17: PowerShell Key Commands

And what that does for us is it gives us, as you can see, all of the objects that are greater than two hundred K. And it's in ascending order of size for all those child objects in the Windows directory. If I were to do it-- let's see here.

Notices

2

Notices© 2014 Carnegie Mellon University

This material is distributed by the Software Engineering Institute (SEI) only to course attendees for their own individual study.

Except for the U.S. government purposes described below, this material SHALL NOT be reproduced or used in any other manner without requesting formal permission from the Software Engineering Institute at [email protected].

This material was created in the performance of Federal Government Contract Number FA8721-05-C-0003 with Carnegie Mellon University for the operation of the Software Engineering Institute, a federally funded research and development center. The U.S. government's rights to use, modify, reproduce, release, perform, display, or disclose this material are restricted by the Rights in Technical Data-Noncommercial Items clauses (DFAR 252-227.7013 and DFAR 252-227.7013 Alternate I) contained in the above identified contract. Any reproduction of this material or portions thereof marked with this legend must also reproduce the disclaimers contained on this slide.

Although the rights granted by contract do not require course attendance to use this material for U.S. government purposes, the SEI recommends attendance to ensure proper understanding.

THE MATERIAL IS PROVIDED ON AN “AS IS” BASIS, AND CARNEGIE MELLON DISCLAIMS ANY AND ALL WARRANTIES, IMPLIED OR OTHERWISE (INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE, RESULTS OBTAINED FROM USE OF THE MATERIAL, MERCHANTABILITY, AND/OR NON-INFRINGEMENT).

CERT ® is a registered mark owned by Carnegie Mellon University.

Page 17 of 17