# Working with Binary in Powershell

Introduction

In most cases when working in Powershell, you may run across raw computer language or occasionally some binary. As we all know, binary comes in sequences of 1′s and 0′s which for us can be very hard to interpret. Thankfully there is a way to manipulate Binary in Powershell to help you along the way. In most cases, you would need to convert your numbers into binary, or vice versa, in order to work with the individual bits of a number. Let's give it a try.

The connection to binary

Let's say we have a number like 1234 that we would like to view the binary version of. Unfortunately, we cannot just input our number and have the computer return what it interprets. There is a connecting language between our numbers and the computer's binary. That number is the Hexadecimal number. Without going into to much detail, each Hexadecimal number within a string holds a byte of information that translates into binary. So how do we turn our number into a Hexadecimal? Simple. By adding a 0x in front of our number. Let's try.

Example

```PS > \$hexNumber=0x1234 //Our stored variable

//enter our named variable and get the output
PS > \$hexNumber
4660
```

Copy and Try it

As you can see, by simply placing a 0x in front of our number, the computer interprets that number as a hexadecimal value. Take note that you do not have to type in PS >. We simply placed that in so you know the beginning line of code that you must input.

Getting the Binary Value

Since we have a number that the computer can understand as stored information instead of just an integer value, it's time to tell the computer to convert our number into a binary number. We will tell the computer to do so by a set of CMDLets to Convert the number into a String and supply it a base of two within the parameters. Let's try our number 1234 (aka hexadecimal 4660) for this example.

Example

```PS > [Convert]::ToString(1234,2) //Our stored variable
//Computer Output
10011010010
```

Copy and Try it

What if Powershell gives us a binary but we need the hexadecimal value. Let's try this method.

Example

```    PS > [Convert]::ToInt32("10011010010",2) //Our stored variable
//Computer Output
1234
```

Copy and Try it

The computer outputted the hexadecimal value 1234. It's as easy as that.

Why does this matter?

The best example of how converting these numbers can help you is invoking a Pozwershell command that gives you a files attributes. Sometime the computer might give you a value like 16417 or 12345 when you request a files attributes. What it is really doing is going through a table with the file attributes and writing True (or 1) or False (or 0) for each attribute. This would give the computer a binary sequence which then translates into a hexadecimal. With that hexadecimal number, you yourself can see from the attributes list what is set to true or false.

Wrapping It Up

Powershell is a great shell command program that will help you administer your computer and servers. If you ever come across and unusual number in your Powershell projects, test out converting it to binary and see if there is a table it is comparing the sequence to and you will most likely find your answer.