Powershell 101

First let me start by saying I have only been using Powershell for a month now.  The learning curve has been quick, there is a lot of great documentation online and for all you .net developers out there you’re in luck, it is build on the .net platform.

Do not be scared of this thing called Powershell, for developers and administrators it is a godsend and will make your work life 10x easier. You can write scripts to manage deployments, run maintenance scripts, or just automate manual tasks.  You can even schedule powershell scripts.

A Little History

Remember Windows Vista, wait don’t force those bad memories. Remember Windows XP SP2 or Windows Server 2003, that’s how old or young Powershell is.  PS V1 has come along way since it was introduced and now includes support for Windows Azure, SQL, Sharepoint, Exchange and a ton of other platforms.  The latest version, 5.0, was released in April 2014.

Hello Powershell

Let’s take a look at some basics functionality you will need to know/understand before you get started.

Declaring a variable: All variables are prefixed with the $ symbol.  Let’s take a look at some examples:

$someInt = 5;
$someString = “something”;
$someArray = “position 0″,”position 1″,”position 2”;

Calling a function: This confused me for a bit, I’m so used to C# where you call SomeMethod(paramA, paramB), with Powershell it is slightly different:

SomeMethod paramA paramB”;

Output to the Console: From what I understand there are multiple ways to output objects to the console.  I use a cmdlet called “Write-Host”.  You can output simple text for logging or you can output objects as well.

Write-Host “Process Started”;
Write-Host $urls;
Write-Host “Completed”

Conditions: Writing conditions are a little bit different from what I was used to with .net.  Instead of using == you need to use -eq.  There is a good post that talks about the different conditions.  Take a look here: http://ss64.com/ps/syntax-compare.html

$myVar -eq 123
“abcdef” -like “abc*”
“abcdef” -replace “dEf”,”xyz”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s