powershellscripts.com

Tutorials  Tutorials

Powershell Basics
Introduction to Windows PowerShell Part 1
Introduction to Windows PowerShell Part 2
Introduction to Windows PowerShell Part 3
PowerShell Script Signing
PowerShell Scheduled Tasks
PowerShell Networking Tasks

Powershell Advanced Tutorials
PowerShell and VMware Introduction



 



Introducing PowerShell

The company that has spent countless billions in the quest for the perfect GUI based OS is now, strangely enough, turning its focus back to  the command line. Their new shell, PowerShell (formerly known as monad) is a completely new interface, which in true Microsoft fashion borrows heavily from the existing shell's (POSIX, Born, etc..) and languages such as Perl while basing it all on Microsoft's proprietary C# object oriented programming language.

This article will be written over the course of the next few weeks and is intended to be a practical introductory guide to PowerShell for those who would like to get a jumpstart into using PowerShell's many practical and useful capabilities without wading through hundreds of pages of MS documentation.  We will jump around a bit and quite possibly skip some of the more obscure methods and syntax that make up PowerShell, so I encourage you to download Microsoft's official documentation here as a supplement to this and any other articles you may read.

Getting Started
We’re not going to get very far without the software, so unless your OS natively included PowerShell with it you’ll need to make a quick download. Microsoft’s download page for PowerShell can be found here.

Once you have it installed you can invoke it by typing PowerShell at the command line, run box, or using the program files shortcut which has been nicely preconfigured with some custom formatting options. You’ll be able to tell you are in the PowerShell by seeing the PS in front of your prompt.

Commands

One of the nice things about PowerShell is that most of your old cmd.exe commands still work. Don’t believe me? Let's go ahead and try some.
	date
	Dir
	Dir > abc.txt
	Type abc.txt
You’ll notice that the date and dir output is not quite what we’re used to but it’s pretty similar.
Ok, that’s great, but what new stuff can it do. Let’s try a few.
	Get-service
	Get-process
	Get-psdrive
To get a quick listing of ALL the new commands or cmdlets as they are now called simply type get-command. If you want help for any of the cmdlets just add a -? to the end of it or try using get-help -detailed for the entire help file. Also using the man command works as you might expect.

Aliases
Typing Get-blah every time you want to get some work done can become a little tedious. Luckily most cmdlets have built in aliases you can call instead. In fact many of the cmdlets have been aliased to commands you already know, for instance every time you call the dir command, you are really running the Get-Children cmdlet. Let’s look at the mappings for some of the more common aliases that are used.
	ls, dir – Get-Childitem
	history, ghy – Get-History
	cat, type – Get-Content
	rm, rmdir, ri,  – Remove-Item
To get a full listing of all the available aliases use get-alias or get-alias | sort to see the list grouped by cmdlet.

Piping Parlor Tricks
Like most shells PowerShell uses pipes | to redirect output from commands to files, variables or additional commands for further processing. Let’s try a couple.
	get-process | format-table –auto
	get-service | format-list
	ls | sort length
	ls | sort length – descending
We’ve only used a single pipe in the examples above however you’ll often want to use multiple pipes, for example to drill down into individual items in your returned data.

PowerCalc?
Another feature new to the windows cmd.exe crowd is the ability to perform arithmetic operations right on the command line.
	5+5
	2*(5+5)
By default the result of your equation will be displayed to standard output. However you can just as easily store the output in either a file or variable.
	$myvar = 2*(5+5)
	$myvar > myfile.txt
	Type myfile.txt
Usability Tips
Tab completion is alive and well in PowerShell and has a few nice tweaks added including an Intellisense like feature that helps select properties on a given object.
	cd c:\(tab)
	cd c:\*files(tab)
	Get-(tab)
	$myvar.(tab)
Up/down arrow and Pg up/down will let you cycle through your history and F7 will bring up a handy popup window from which you can cycle through and select one of your previously entered commands.

Conclusion
We’ve covered a very small subset of the functionality of Microsoft’s new shell, but hopefully your interest is peaked and you’re ready to dive into some greater detail. In our next article we’ll cover PowerShell's general syntax and see how it handles traditional programming constructs such as variables, iterations, and selection.  Next Article