Using PowerShell to Demo C# and .NET

I have an intern working with us and he is writing some PowerShell and C# WinForms to automate some of our administrative tasks. He had a problem with a C# app and when he started asking me about it, it sounded like a hash table was just what the doctor ordered.

He had seen hash tables in PowerShell but wasn't quite sure how they would work in C# (he had only cracked open C# maybe 5 weeks ago). Rather than firing up Visual Studio and typing in a bunch of "blah-blah/yadda-yadda" I was able to demo a "C# version" of hash tables in PowerShell.

Once he saw this, he was able to understand how to use hash tables in C#. 

   1: 268 >  $hash = New-Object System.Collections.Hashtable
   2: 269 >  $hash.Add("one",1)
   3: 270 >  $hash
   4:  
   5: Name                           Value
   6: ----                           -----
   7: one                            1
   8:  
   9:  
  10: 271 >  $hash.one
  11: 1
  12: 272 >

I just love how PowerShell is continuing to bridge a gap between Developers and Admins. These are obviously different disciplines and both require different skills, but the more we can learn about each one, the more effective we can be in our respective roles.

Comments (4) -

I agree that PowerShell can be a great catalyst for Developers and Admins to gain more of an understanding and respect for each others' disciplines.

Now we need some more admin presence at .NET focused user groups and events!

One big thing I see in the PowerShell community is the quest for brevity, especially when counting keystrokes. The one-liner is always highly-praised.
To that end, I wanted to share a quicker way to create a hash variable.

Rather than:  $hash = New-Object System.Collections.Hashtable

You can simply type: $hash = @{}

Hey Derek,

You are absolutely right. I love the brevity of PowerShell and I typically use that syntax to create Hash Tables. as well

However, the point I was trying to make in the article is that you can use PowerShell to demo some .NET stuff in a similar way that a developer would create a hash table in C#.

If I had told my intern that you could create a hash table in Powershell using the @{} syntax, he would never have been able to take that and use it to create a hash table in his C# app.

Using the entire fully qualified name of the class in my demo to him leads him to be able to use hash tables in C#.

I figured that was the reason behind the example as you gave it. Using the New-Object example does make it a much clearer demo of what can be done.

Re-reading your post, it sounds like your guy has about... well.... 5 weeks on me when it comes to C#, so he's already an expert in my book!  Smile

PowerShell is great in its own right. It is cool to see that it can also be used as a teaching aid for other languages. Fun stuff!

Thanks for the post and blog! I'm a subscriber for life!

Comments are closed