Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

Easily Update PowerShell Type Data by Using a Cmdlet

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to easily update Windows PowerShell type data by using a built-in cmdlet.

Microsoft Scripting Guy, Ed Wilson, is here. This morning, the Scripting Wife and I are heading back to Charlotte, North Carolina. We were in Raleigh, North Carolina, so I could speak at the Triangle SQL Server User Group meeting last night. It is a pretty good drive from Charlotte to Raleigh, and it is much safer to spend the night and make the drive in the morning. We had a great time, and we were able to see many of our friends.

I spoke at the SQL Server Saturday event in Raleigh, and I have spoken at their user group meetings before. They have a fine group, and do a lot of great work for the community. The Scripting Wife and I are now focusing on the Windows PowerShell Saturday #003 in Atlanta, Georgia, on October 27, 2012. Of course, October 26, 2012 is when Microsoft Surface is supposed to be available, and Teresa is chomping at the bit to get to the Microsoft Store prior to Windows PowerShell Saturday. At first, she was planning on going to the Microsoft Store in Atlanta on Friday, but now she is angling to get to the Charlotte Microsoft Popup Store prior to our road trip. I will let you know how all that works out.

In Windows PowerShell, I have always had the ability to update type data. It involved monkeying around with XML files—but it could be done. In Windows PowerShell 3.0, we have made it really easy to update type data. We even use the same cmdlet, Update-TypeData, to perform this operation.

Background: What is a type?

Everything in Windows PowerShell is an object. A simple string such as “this is a string” is an instance of a data type—it is an instance of the System.String .NET Framework class. A date, such as “October 10, 2012” is an instance of the System.DateTime .NET Framework class. Because everything is an object in Windows PowerShell, the language is very powerful, but it can also become rather complicated. The object-oriented nature of Windows PowerShell also permits adding additional information to an object. This is called updating type data, and it is an advanced Windows PowerShell topic.

Doing things the old way

Perhaps the best way to understand about updating type data in Windows PowerShell is to see what one might need to do without the ability to update type data. This time of year is always a bit tricky because of the impending time change—you know, spring forward, fall back. Unfortunately, this reversion back from daylight savings time does not happen at the same time all over the world. Luckily, there is a method available from the System.DateTime object that returns a simple Boolean value. The method, which is called IsDayLightSavingTime, is available on a DateTime object, and it is accessible as follows.

(get-date).IsDayLightSavingTime()

Although this is useful, it is an extra step, and it would be easier to access if it was a property instead of a method.

Adding a property to an existing object

To add a property to an existing object in Windows PowerShell 3.0, all I need to do is use the Update-TypeData cmdlet. I specify the data type I wish to update, the name of the new member, the type (here I use ScriptProperty), and the value. The cool thing about ScriptProperty is that I can use a script block to calculate (or to call a method) to create the value. The command is shown here.

Update-TypeData -TypeName system.datetime -MemberName isDST -MemberType scriptproperty -Value {$this.isDaylightSavingTime()}

When I call the Get-Date cmdlet, the new property isDST appears. This permits easy access to see if the current time on the target computer is daylight savings time (thereby letting you know if it has already fallen back). The command and the associated output are shown in the image that follows.

Image of command output

If I want to change things, I can use the Remove-TypeData cmdlet as shown here.

Remove-TypeData -TypeName system.datetime

If I want the additional property to be available on the System.DateTime objects on a regular basis, all I need to do is to add the Update-TypeData command to my Windows PowerShell profile.

Note   I have an entire series of Hey, Scripting Guy! Blog posts that talk about working with Windows PowerShell profiles.

That is all there is to updating type data in Windows PowerShell 3.0. I will talk tomorrow about more cool Windows PowerShell stuff—hope to see you then.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>