{"id":10721,"date":"2006-04-25T12:18:00","date_gmt":"2006-04-25T12:18:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/base64-encodedecode-a-string\/"},"modified":"2019-02-18T13:25:00","modified_gmt":"2019-02-18T20:25:00","slug":"base64-encodedecode-a-string","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/base64-encodedecode-a-string\/","title":{"rendered":"Base64 Encode\/Decode a string"},"content":{"rendered":"<p>Today I came across <a href=\"http:\/\/scripts.readify.net\/\">http:\/\/scripts.readify.net\/<\/a>&nbsp; . This sight is focused on Monad and MSH and is starting a collection of scripts at: <a href=\"http:\/\/scripts.readify.net\/Scripts.aspx\">http:\/\/scripts.readify.net\/Scripts.aspx<\/a>&nbsp;.&nbsp; You should visit their site and let them know what type of scripts would be useful to you.&nbsp; <\/p>\n<p>I particularly liked their entry on how to Base64 encode a file.&nbsp; This is something that I need occassionally and I can never remember how to do it.&nbsp; I was about to include it as-is into my profile and decided that there was a better way to do this.&nbsp; <\/p>\n<p>&lt;IMPORTANT POINT&gt; <br \/><font color=\"#ff0000\"><strong>Whenever you are adding some functions, you should make a conscious decision about whether those functions are best exposed as a &#8220;function&#8221; or as a &#8220;type extension&#8221;.<br \/><\/strong><\/font>&lt;\/IMPORTANT POINT&gt;<\/p>\n<p>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.&nbsp; One of the huge benefits of type extensions is discoverability.&nbsp; Let me show you how to do this function as a type-extension and highlight the benefits of this approach.<\/p>\n<p>When doing a type extension, you first need to decide what TYPE you are going to extend.&nbsp; In this case, I decided that I would extend SYSTEM.STRING as that was the most general purpose type for this function.&nbsp; I then encoded the following in a file called My.Types.Mshxml<\/p>\n<p>&lt;\/Types&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;Type&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;System.String&lt;\/Name&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Members&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ScriptProperty&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;ToBase64String&lt;\/Name&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;GetScriptBlock&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($this))<br \/>&nbsp;&nbsp;&lt;\/GetScriptBlock&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/ScriptProperty&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ScriptProperty&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;FromBase64String&lt;\/Name&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;GetScriptBlock&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this))<br \/>&nbsp;&nbsp;&lt;\/GetScriptBlock&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/ScriptProperty&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/Members&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;\/Type&gt;<br \/>&lt;\/Types&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>In my profile I load this file via the command:<\/p>\n<p>&nbsp;&nbsp;&nbsp;<strong>Update-TypeData c:\\msh\\My.Types.mshxml<\/strong><\/p>\n<p>Once I do that, these properties are available to any STRING and can be discovered via Get-Member:<\/p>\n<p>MSH&gt; <strong>$x=&#8221;Hello World&#8221;<br \/><\/strong>MSH&gt;<strong> $x |Get-Member<\/strong><\/p>\n<p>&nbsp;&nbsp; TypeName: System.String<\/p>\n<p>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Definition<br \/>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<br \/>Clone&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Object Clone()<br \/>CompareTo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Int32 CompareTo(Object value), System.Int32 CompareTo(String strB)<br \/>Contains&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Boolean Contains(String value)<br \/>CopyTo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Void CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIndex, Int32 count)<br \/>EndsWith&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Boolean EndsWith(String value), System.Boolean EndsWith(String value, StringComparison comparisonType), System.Boolean EndsWith(String value, Boolean ignoreCase, CultureInfo culture)<br \/>&#8230;<\/p>\n<p>Trim&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String Trim(Params Char[] trimChars), System.String Trim()<br \/>TrimEnd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String TrimEnd(Params Char[] trimChars)<br \/>TrimStart&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.String TrimStart(Params Char[] trimChars)<br \/>Chars&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ParameterizedProperty System.Char Chars(Int32 index) {get;}<br \/>Length&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Int32 Length {get;}<br \/><font color=\"#ff0000\"><strong>FromBase64String<\/strong> ScriptProperty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Object FromBase64String {get=[System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this));}<br \/><strong>ToBase64String<\/strong>&nbsp;&nbsp; ScriptProperty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Object ToBase64String {get=[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($this));}<\/font><\/p>\n<p>MSH&gt; <strong>$x<br \/><\/strong>Hello World<br \/>MSH&gt; <strong>$x.ToBase64String<\/strong><br \/>SABlAGwAbABvACAAVwBvAHIAbABkAA==<br \/>MSH&gt;<strong> $x.ToBase64String.FromBase64String<br \/><\/strong>Hello World<br \/>MSH&gt; <\/p>\n<p>&nbsp;<\/p>\n<p>Enjoy!<\/p>\n<p>Jeffrey Snover<br \/>Monad Architect<\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n<p>PSMDTAG:CMDLET: Update-TypeData<\/p>\n<p>PSMDTAG:FAQ: How do I base64 encode\/decode a string?<\/p>\n<p>PSMDTAG:PHILOSOPHY<font color=\"#000000\"><strong>:<\/strong> Whenever you are adding some functions, you should make a conscious decision about whether those functions are best exposed as a &#8220;function&#8221; or as a &#8220;type extension&#8221;.<\/font><\/p>\n<p><font color=\"#000000\">PSMDTAG:DOTNET: System.Convert, System.Text.Encoding<\/font><\/p>\n<p>PSMDTAG:TYPEEXTENSION: System.String<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I came across http:\/\/scripts.readify.net\/&nbsp; . This sight is focused on Monad and MSH and is starting a collection of scripts at: http:\/\/scripts.readify.net\/Scripts.aspx&nbsp;.&nbsp; You should visit their site and let them know what type of scripts would be useful to you.&nbsp; I particularly liked their entry on how to Base64 encode a file.&nbsp; This is [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[12,14,10,11,15],"class_list":["post-10721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-cmdlet","tag-dotnet","tag-faq","tag-philosophy","tag-typeextension"],"acf":[],"blog_post_summary":"<p>Today I came across http:\/\/scripts.readify.net\/&nbsp; . This sight is focused on Monad and MSH and is starting a collection of scripts at: http:\/\/scripts.readify.net\/Scripts.aspx&nbsp;.&nbsp; You should visit their site and let them know what type of scripts would be useful to you.&nbsp; I particularly liked their entry on how to Base64 encode a file.&nbsp; This is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10721"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10721\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}