ProgrammingJavascriptCareerProductivityGadgetsASP.NETWeb Design
for beginners

Getting Started With Windows Powershell

Written by
Published on
Modified on
Filed under
Getting Started With Windows Powershell

If you like Windows scripting, then you're in for a treat. If you hate scripting, then maybe Windows Powershell can help change your mind. Windows Powershell is a automation and configuration management framework made by Microsoft that comes installed with Windows 7+ and can help with many of your automation tasks. If you're familiar with the Windows Command Line Interface, then you'll be familiar with Powershell. It is essentially the same thing, but taken up several notches. It gives users full access to COM and WMI components allowing you to perform administrative tasks on local and/or remote computers.

Personally, I use it to create automated scripts that I run daily to perform several services for my personal websites. Everything from generating reports to sending out emails to killing processes in the background that may be taking up resources. So if you're interested so far, then read on. This is only a getting started guide, and as such I won't touch on process manipulation just yet, but in a future post.

Launch Powershell

If you're running Windows 7, 8, or 8.1 then you have Powershell pre-installed on your machine already. You can do a Windows search to look for it, or you can find it in the start menu by selecting "All Programs", then navigating over to your "Accessories" directory, at the very bottom you'll see the "Windows Powershell" directory, which will then reveal the following 4 options.

Some people think it's too hidden, but it's in the exact same location that the Command Prompt so, so it makes sense. If you're running a 64 bit OS then you'll see 4 options, 2 Powershell options and 2 ISE options, more on those down below. For now you can click on the Windows Powershell (x86) option to get started. Once you do, you should see the following in all of its glory.

The Powershell interface looks just like the Command Line Interface, but it's blue and actually is pretty advanced once you start to play around with it. For example, it offers full auto-complete, and command scrolling with the tab key, and text highlighting for copy and paste purposes. The screen above shows the Get-Help command output which if you're starting off you're going to spend alot of time in.

One of the best features of Powershell in my opinion is the robust built in documentation for every command. So if you're ever confused, just:

Get-Help "what I'm confused about"

But the even cooler feature, is that the documentation also comes with full examples, so you don't have to sit there guessing at what to type next. Let's say for example that we wanted to know more about how the Get-Help command worked. We could simply:

Get-Help Get-Help -examples

This would bring up several examples showing which parameters to input and the format of the commands. Super useful for someone who is starting off with Powershell.


Basic Commands

If you're familiar with the Windows Command Line then you'll be able to do those exact same tasks with Powershell. For example, all of the following commands will run just as expected:

  • cls - clears screen
  • dir - display directory listing
  • cd - change directory
  • md - make directory
  • ping - ping domain
  • exit - exit shell

So if you're still using the standard Windows Command Line, then quit it and hop on over to Powershell. You'll lose nothing, and you'll have an entire new set of functionality available at your disposal.


Cmdlts

Cmdlts are the commands that are built for Powershell, which come in 2 part names (Verb-Noun). We saw an example of that above with the Get-Help command. Get being the verb and Help the noun. You can view a few of the many commands down below.

  • Get-Help
  • Get-History
  • Get-Process
  • Get-Service
  • Get-Verb

Let's take a look at one cmdlt right now, in order to see how they work. We'll try out the Get-History command as it's pretty self explanatory and has a useful feature called an Alias associated with it.


The first thing to notice is that the Powershell commands are not case-sensitive, but to follow the standard that all of the documentation and built in autocomplete has, you should stick with camel case. And secondly, the awesome Get-History command, which returns all of the commands that you've typed since the Powershell session started. By default, you can only get a maximum of 64 records to display, which you can view with:

Get-Variable -Name MaximumHistoryCount

You can set this value to something else if you need to with:

Set-Variable -Name MaximumHistoryCount -Value 128

You can see that the set command follows the same Verb-Noun naming structure that we've been working with up to now. An interesting note is that the maximum allowable limit that you can set is 32767, which tells me that it is a 16 bit signed integer. Good to know.

One last note, you can get the exact same results from above by just typing:

h

This is a Powershell Alias, and is shorthand for Get-History. Another alias that you can use is just history. But why type history when you could just type h. Many commands in Powershell have Aliases, and you can view the rest for yourself with:

Get-Alias

The Script Editor

A cool feature of Powershell is that it comes with its very own Script Editor. If you look back at the directory listing above showing the 4 files associated with Powershell, the script editor would be Windows Powershell ISE program. You must be running at a minimum the .NET Framework 3.5, which I don't know why you wouldn't, in order to run it. The script editor is perfect for testing out scripts and commands before pushing them live.

The ISE consists of 3 sections, the editor in which you'll be typing in your commands, the output panel which executes the commands and shows the output, and the shell interface at the very bottom, which will run any command that you can type into it, which is super convenient.



Important note: By default you cannot run scripts in Windows Powershell. You can verify this by typing the following command into the interface.

Get-ExecutionPolicy

Which will return Restricted by default. This is mainly due to the fact that most people won't be creating and running scripts, they'll be interacting with Powershell directly through the shell interface. You can change the execution policy by running Powershell with administrative privileges and using the following:

Set-ExecutionPolicy

This was just a quick guide for anyone who is thinking of trying out Powershell, or someone who is need of Windows task management. There's tons of features that you can make use of with Powershell, many of which I'll be covering in future posts. Evrything from controlling processes and services to running and creating tasks. Any questions, feel free to drop them down below.

Walter Guevara is a software engineer, startup founder and currently teaches programming for a coding bootcamp. He is currently building things that don't yet exist.

Comments

No messages posted yet

Developer Poll

Q:

Stay up to date

Sign up for my FREE newsletter. Get informed of the latest happenings in the programming world.

Add a comment

Keep me up to date on the latest programming news
Add Comment

Stay up to date

Get informed of the latest happenings in the programming world.

No thanks