Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Playing Around with Format-Hex

$
0
0

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 standard parameters. Other cmdlets that use parameters with similar names generally take pains to ensure that those parameters work the same way.

For example, the Pathparameter is not a standard parameter. Yet, it is usually always called Path, 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 Path. Many times, Patheven accepts an array of paths—although sometimes it will not.

So how do I know?

I like to use a combination of Get-Help and Get-Command. 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:

Get-Help Format-Hex

One of the cool things about Windows PowerShell 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.

This default Help for the Format-Hex cmdlet is shown here:

Image of command output

If I want to pipe a string to the Format-Hex cmdlet (that would be the input object), I retrieve a hex representation of the asci data that is in the string:

Image of command output

I can also read the contents of a file and display the hex value of the content of that file by supplying a path:

Image of command output

If I look at the content of the text file, I can compare it to my previous string. The string that I piped to Format-Hex 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—only the last five hex values from the text file are different. This is shown here:

Image of command output

So, what kind of an object am I returning from the Format-Hex cmdlet? Well, it is a byte collection. Here are the members:

PS C:\> 'A file with a `in the name.' | format-hex | Get-Member

   TypeName: Microsoft.PowerShell.Commands.ByteCollection

Name        MemberType Definition

----        ---------- ----------

Equals      Method     bool Equals(System.Object obj)

GetHashCode Method     int GetHashCode()

GetType     Method     type GetType()

ToString    Method     string ToString()

Bytes       Property   byte[] Bytes {get;}

Offset      Property   uint32 Offset {get;}

Path        Property   string Path {get;}

This means, that I can compare the bytes. I store the objects to variables, and then use Compare-Object to do a comparison:

Image of command output

I can even search for stuff in my hex output. For example, I can look for "file":

PS C:\> format-hex 'C:\fso\A File with a `in the name.txt' | Select-String "file"

00000000   41 20 66 69 6C 65 20 77 69 74 68 20 61 20 60 69  A file with a `i

That is all there is to using the Format-Hex cmdlet in Windows PowerShell. Join me tomorrow when I will talk about more cool stuff.

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 3333

Trending Articles



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