{"id":74141,"date":"2015-08-06T00:01:00","date_gmt":"2015-08-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/08\/06\/playing-around-with-format-hex\/"},"modified":"2019-02-18T09:46:42","modified_gmt":"2019-02-18T16:46:42","slug":"playing-around-with-format-hex","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/playing-around-with-format-hex\/","title":{"rendered":"Playing Around with Format-Hex"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about the Format-Hex cmdlet in Windows PowerShell 5.0.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that it usually just works. Well of course, after you learn how, it just works. For example, it is largely self-describing, and it has standard parameters. Other cmdlets that use parameters with similar names generally take pains to ensure that those parameters work the same way.<\/p>\n<p>For example, the <b>Path<\/b><i> <\/i>parameter is not a standard parameter. Yet, it is usually always called <b>Path<\/b>, and it generally points to the path to something. In addition, I can generally supply relative and literal paths, PSPaths, and often even UNC paths to a parameter with a name of <b>Path<\/b><i>. <\/i>Many times, <b>Path<\/b><i> <\/i>even accepts an array of paths&mdash;although sometimes it will not.<\/p>\n<p>So how do I know?<\/p>\n<p>I like to use a combination of <b>Get-Help<\/b> and <b>Get-Command<\/b>. If a cmdlet does not have any formal Help associated with it, it will still provide a basic sort of help that provides a nicely formatted syntax. Often, that is all that is required. Here is an example:<\/p>\n<p style=\"margin-left:30px\">Get-Help Format-Hex<\/p>\n<p>One of the cool things about Windows PowerShell&nbsp;5.0 Help is that it displays aliases for the cmdlets. In addition, the Help displays three parameter sets (or ways of using the cmdlet). I can provide a path, a literal path, or an input object. If I provide an input object, I can also specify encoding.<\/p>\n<p>This default Help for the <b>Format-Hex<\/b> cmdlet is shown here:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-8-6-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/HSG-8-6-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I want to pipe a string to the <b>Format-Hex<\/b> cmdlet (that would be the input object), I retrieve a hex representation of the asci data that is in the string:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-6-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-6-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can also read the contents of a file and display the hex value of the content of that file by supplying a path:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-6-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-6-15-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If I look at the content of the text file, I can compare it to my previous string. The string that I piped to <b>Format-Hex<\/b> is the same as the string that is contained in the text file. If I look at the hex data from the string and the text file, I can see that it is very similar&mdash;only the last five hex values from the text file are different. This is shown here:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-6-15-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-6-15-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>So, what kind of an object am I returning from the <b>Format-Hex<\/b> cmdlet? Well, it is a byte collection. Here are the members:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; &#039;A file with a `in the name.&#039; | format-hex | Get-Member<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; TypeName: Microsoft.PowerShell.Commands.ByteCollection<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">Equals&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; bool Equals(System.Object obj)<\/p>\n<p style=\"margin-left:30px\">GetHashCode Method&nbsp;&nbsp;&nbsp;&nbsp; int GetHashCode()<\/p>\n<p style=\"margin-left:30px\">GetType&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; type GetType()<\/p>\n<p style=\"margin-left:30px\">ToString&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; string ToString()<\/p>\n<p style=\"margin-left:30px\">Bytes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; byte[] Bytes {get;}<\/p>\n<p style=\"margin-left:30px\">Offset&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; uint32 Offset {get;}<\/p>\n<p style=\"margin-left:30px\">Path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; string Path {get;}<\/p>\n<p>This means, that I can compare the bytes. I store the objects to variables, and then use <b>Compare-Object<\/b> to do a comparison:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-6-15-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-6-15-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can even search for stuff in my hex output. For example, I can look for &quot;<b>file&quot;<\/b>:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; format-hex &#039;C:\\fso\\A File with a `in the name.txt&#039; | Select-String &quot;file&quot;<\/p>\n<p style=\"margin-left:30px\">00000000&nbsp;&nbsp; 41 20 66 69 6C 65 20 77 69 74 68 20 61 20 60 69&nbsp; A file with a `i<\/p>\n<p>That is all there is to using the <b>Format-Hex<\/b> cmdlet in Windows PowerShell. Join me tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about the Format-Hex cmdlet in Windows PowerShell 5.0. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that it usually just works. Well of course, after you learn how, it just works. For example, it is largely self-describing, and it has [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[609,3,608,45],"class_list":["post-74141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powershell-5","tag-scripting-guy","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about the Format-Hex cmdlet in Windows PowerShell 5.0. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that it usually just works. Well of course, after you learn how, it just works. For example, it is largely self-describing, and it has [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=74141"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74141\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=74141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}