How To Run Powershell Commands

Powershell is a shell that allows you to run commands. In this chapter, I will show you how to make the most of this.




Introduction



Running commands is the most basic way to use Powershell for beginners. It is how most people get started. We often have a quick task or need some information quickly on a remote machine.


These kind of uses are ideal for shells. They are ideal because they are quick and reliable. I was a Linux user long before Powershell existed. So when I needed to work on a Windows machine, the first thing I did was learn its shell environment.


Others may look for a tool with a gui and that is perfectly acceptable if that is what you want. However, when you get used to working with shells and their advantages, you never want to use anything else.



Running A Command



A Powershell command always has a verb-noun structure. Something like:


get-content

It is done like this so it is relatively easy to know what the command does. 

You can use the help system to learn how to use a particular command. This will probably be a very often used technique. There have been many times when I have forgot a command and just had to search for it.

So I do appreciate having this ability built in to the system. It has helped me greatly, as I am fairly forgetful at times.


get-help get-content -examples


This will give you several details and a few good examples of the command. It does not go into the full depth of the command. That is an option though. The help system will also tell you of any required parameters.


If it is required, then you must supply that information in order for the command to run. If any parameter includes spaces then you will need to enclose it in quotation marks. Commands are also not case sensitive.



Using Aliases


Aliases can be a convenient way to remember long commands. It is one of the powershell essentials that will always be useful. Some of these long commands are just a pain. In these instances, using an alias makes a lot of sense. It is what they are for.


There is a nice way to find out if a command has an alias. So, if it is a pain to type or you type it a lot, then try this.


get-alias -definition get-content


This does indeed tell us there is an alias for the "get-content" command.
It is "gc".


If you need to know what an alias is, then there is a way for that also. I know I can never remember anything but the most basic ones. I end up having to use this a lot. People like to use aliases when they write code.


help gc


This will give you the full command and all of its details.

 

Useful Commands



For the rest of this chapter I want to go over some of the more useful commands. I want to do this so you will start to become familiar as soon as possible with them.


get-childitem


This command will list files in a certain location. Use it like this.


get-childitem c:/Documents


This is useful when you want to check if there is anything in a directory. You can use the "-recurse" option to list subfolders and files.


copy-item


Thanks to Powershell's naming scheme, it is pretty easy to figure out what most commands do. This is a good example. Copying an item to another location is easy with this command.


get-childitem c:\users\username -recurse | copy-item -destination c:\folder


This reads the information in with the previous command and copies those items to another folder. That's pretty useful!


export-csv


When you need reports of things, this is the command to use. It will take the output of another command and make a "csv" file of it. Let's try a few.


get-process | export-csv -path c:\export\process_report.csv


Put that in your shell and you will get a lot of information about the processes running on your system right now. It will be a file in the folder you just specified. We can make it look a little nicer so lets do that.


get-process | export-csv -notypeinformation -path c:\export\process_report.csv


That extra flag will hide some of the useless stuff that comes with the file. Now let's customize the information. We can use the "select-object" command to select the columns we want. Let's just see the handles processname.


get-process | select-object handles, processname |export-csv -path c:\export\process_report.csv


If we needed just those two columns for some reason, then we would have them. This is a useful command and I have also shown how to link different commands together.



Conclusion



In this chapter we started with how to run a basic command. These commands were basic forms with no parameters added. We then took a look at aliases and how they can be useful. Afterwards, we even went over how to create your own alias.


Then I showed some of these basic commands in action. They are all nice to know and can be very helpful in the right situations. I gave examples and even showed how to create some files with your reporting.