Base64 Encode/Decode a string

Today I came across  . This sight is focused on Monad and MSH and is starting a collection of scripts at: .  You should visit their site and let them know what type of scripts would be useful to you. 

I particularly liked their entry on how to Base64 encode a file.  This is something that I need occassionally and I can never remember how to do it.  I was about to include it as-is into my profile and decided that there was a better way to do this. 

Whenever you are adding some functions, you should make a conscious decision about whether those functions are best exposed as a "function" or as a "type extension".

Both of these mechanisms are great and have their purpose but my observation is that people are not using type-extensions as much as they should.  One of the huge benefits of type extensions is discoverability.  Let me show you how to do this function as a type-extension and highlight the benefits of this approach.

When doing a type extension, you first need to decide what TYPE you are going to extend.  In this case, I decided that I would extend SYSTEM.STRING as that was the most general purpose type for this function.  I then encoded the following in a file called My.Types.Mshxml



In my profile I load this file via the command:

   Update-TypeData c:\msh\My.Types.mshxml

Once I do that, these properties are available to any STRING and can be discovered via Get-Member:

MSH> $x="Hello World"
MSH> $x |Get-Member

   TypeName: System.String

Name             MemberType            Definition
----             ----------            ----------
Clone            Method                System.Object Clone()
CompareTo        Method                System.Int32 CompareTo(Object value), System.Int32 CompareTo(String strB)
Contains         Method                System.Boolean Contains(String value)
CopyTo           Method                System.Void CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIndex, Int32 count)
EndsWith         Method                System.Boolean EndsWith(String value), System.Boolean EndsWith(String value, StringComparison comparisonType), System.Boolean EndsWith(String value, Boolean ignoreCase, CultureInfo culture)

Trim             Method                System.String Trim(Params Char[] trimChars), System.String Trim()
TrimEnd          Method                System.String TrimEnd(Params Char[] trimChars)
TrimStart        Method                System.String TrimStart(Params Char[] trimChars)
Chars            ParameterizedProperty System.Char Chars(Int32 index) {get;}
Length           Property              System.Int32 Length {get;}
FromBase64String ScriptProperty        System.Object FromBase64String {get=[System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this));}
ToBase64String   ScriptProperty        System.Object ToBase64String {get=[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($this));}

MSH> $x
Hello World
MSH> $x.ToBase64String
MSH> $x.ToBase64String.FromBase64String
Hello World



Jeffrey Snover
Monad Architect

Comments (10)

  1. Michael McMullen says:

    I love this.  Today I was reading Mitch Denny’s blog (he works for Readify) and saw his post about the new scripts site ( and so I subscribed to it.  I then followed one of the other links from his blog and more links from there, etc eventually finding my way to this site which I also subscribed to.  (I’d seen you on channel9 before but hadn’t come accross the blog before today).  And now, the first thing you blog after I subscribe is a link straight back to the blog that started my little MSH journey ealier today.

  2. Oisin G. says:

    Great stuff Jeffrey — it’s little posts like these that draw the curtain back enough to let the light in, so to speak. I’d rather 50 of these posts than 50Mb of dry MSDN pages.

    cmd is dead, long live msh!

  3. Thanks for giving us way to extend Monad 🙂

    Sometimes, when i use get-credential, i usually check for the unsecure, clear string of the credential password, which is of type System.Security.SecureString(refer to Mow’s blog

    I know that it’s not safe to reveal the securestring but I just wanted to extend this script property to SecureString type to view the unsecureString easily without having to specify a function.

    Here is the type I have added.












    Now I do the following to get the unsecure string

    MSH>$admin = get-credential




    clear password displayed here

  4. Welcome!!!”> google pr. [URL=]pagerank 5[/URL]: Web Site Analysis, SE marketing, High Rankings. Also [url=]online pr16[/url] from google pr .

  5. hello! google pr. Web Site Analysis, SE marketing, High Rankings. From google pr .

  6. Welcome!!!”> online directory. [URL=]YP national[/URL]: SMART Yellow Pages, About DIRare, Search in Business Category. Also [url=]global directory[/url] from online directory .

  7. hello! online directory. SMART Yellow Pages, About DIRare, Search in Business Category. From online directory .

Skip to main content