Brian Hartsock's Blog

Intro to Powershell, Part 2 – Types

by bhartsock on Feb.02, 2009, under Uncategorized

In the previous installment, I discussed 3 basic commands that help you navigate around Powershell. This post is focused on the data Powershell makes available through commands, and how you can manipulate it. Types are where Powershell starts to look more like a scripting language than just a shell. First a few basics.

Powershell is object-oriented. This deviates from most shells like Cmd or Bash, where it is really string based. Don’t worry though, it is built on top of .NET, so the types are easy to learn if you have worked with VB.NET or C#.NET at all.

Powershell is dynamically typed. For all you .NET’ers out there, pay attention. Dynamic typing means type decisions are made at runtime, not compile time. So you don’t have to worry about declaring types in your scripts, the engine will figure it out for you.

But enough talking, lets see what this really means.

Working with DateTime type

Let’s look at the get-date command.

get-date

As expected, it just outputs the current date as a string. But if Powershell is object-oriented, why would it just return a string, and not a DateTime object?

2-1-2009-10-38-07-pm

(get-date).GetType()

We use the parenthesis operator to group the output of the get-date command so we can work with it. I will talk more about operators later. But as you can see below, the command actually returns a DateTime object, like we thought it should.

2-1-2009-10-46-59-pm

get-member command

Having a dynamically typed language creates a problem though, we don’t have intellisense, or any idea what functions or members an object provides. This can be very annoying, but Powershell ships with another command, get-member which really helps us out. We have to use the parenthesis to group the output of the get-date command like above.

get-member -inputObject (get-date)

Now we can see all the members and functions available on this object, so we don’t have to flip through MSDN documentation every time we want to work with the object.

2-1-2009-10-46-06-pm

If you want to know more about the get-member command, just use the get-help trick I talked about last time.

get-help get-member

Put it all together

So let’s actually see what we can get out of get-date.

(get-date).year
(get-date).month
(get-date).day
(get-date).hour
(get-date).minute
(get-date).second
(get-date).dayofweek

2-1-2009-10-50-48-pm

You might have noticed something a little odd in the above example. Powershell is case insensitive. This is also another deviation from normal .NET.

What’s this mean?

When working with commands, look at the type of the objects being outputed? Commands like dir/ls/Get-ChildItem are actually returning an enumerable listing of a directory. You don’t have to parse the string output to work with it, you can actually work with it as an object.

I know this seems very basic, but hopefully it will give a good foundation for future posts.

Post to Twitter Post to Digg Post to Facebook Post to Reddit

:

Leave a Reply

Post by day

February 2009
M T W T F S S
« Jan   Mar »
 1
2345678
9101112131415
16171819202122
232425262728