Problem
You want to work with the individual bits of a number, or work with a number built by combining a series of flags.
Solution
To directly enter a hexadecimal number, use the 0x prefix:
PS >$hexNumber = 0x1234
PS >$hexNumber
4660 To convert a number to its binary representation, supply a base of 2 to the [Convert]::ToString() method:
PS >[Convert]::ToString(1234, 2)
10011010010 To convert a binary number into its decimal representation, supply a base of 2 to the [Convert]::ToInt32() method:
PS >[Convert]::ToInt32("10011010010", 2)
1234
To manage the individual bits of a number, use PowerShell’s binary operators. In this case, the Archive flag is just one of the many possible attributes that may be true of a given file:
PS >$archive = [System.IO.FileAttributes] "Archive" PS >attrib +a test.txt PS >GetChildItem | Where { $_.Attributes band $archive } | Select Name
Name
test.txt
PS >attrib a test.txt PS >GetChildItem | Where { $_.Attributes band $archive } | Select Name PS >
Discussion
In some system administration tasks, it is common to come across numbers that seem to mean nothing by themselves. The attributes of a file are a perfect example:
PS >(GetItem test.txt).Encrypt() PS >(GetItem test.txt).IsReadOnly = $true PS >[int] (GetItem test.txt force).Attributes 16417 PS >(GetItem test.txt force).IsReadOnly = $false PS >(GetItem test.txt).Decrypt() PS >[int] (GetItem test.txt).Attributes 32
What can the numbers 16417 and 32 possibly tell us about the file?
The answer to this comes from looking at the attributes in another light—as a set of features that can be either True or False. Take, for example, the possible attributes for an item in a directory shown by Example 63.
Example 63. Possible attributes of a file
PS >[Enum]::GetNames([System.IO.FileAttributes]) ReadOnly Hidden System Directory Archive Device Normal Temporary SparseFile ReparsePoint Compressed Offline NotContentIndexed Encrypted
If a file is ReadOnly, Archive, and Encrypted, then you might consider this as a succinct description of the attributes on that file:
ReadOnly = True Archive = True Encrypted = True
It just so happens that computers have an extremely concise way of representing sets of true and false values—a representation known as binary. To represent the attributes of a directory item as binary, you simply put them in a table. We give the item a “1” if the attribute applies to the item and a “0” otherwise (see Table 61).
Table 61. Attributes of a directory item
Attribute
True (1) or False (0)
Encrypted
1
NotContentIndexed
0
Offline
0
Compressed
0
ReparsePoint
0
SparseFile
0
Temporary
0
Normal
0
Device
0
Archive
1
Directory
0
<Unused>
0
System
0
Hidden
0
ReadOnly
1
If we treat those features as the individual binary digits in a number, that gives us the number 100000000100001. If we convert that number to its decimal form, it becomes clear where the number 16417 came from:
PS >[Convert]::ToInt32("100000000100001", 2) 16417
This technique sits at the core of many properties that you can express as a combination of features or flags. Rather than list the features in a table, though, documentation usually describes the number that would result from that feature being the only one active—such as FILE_ATTRIBUTE_REPARSEPOINT = 0x400 . Example 64 shows the various representations of these file attributes.
Example 64. Integer, hexadecimal, and binary representations of possible file attributes
PS >$attributes = [Enum]::GetValues([System.IO.FileAttributes]) PS >$attributes | SelectObject ` >> @{"Name"="Property"; >> "Expression"= { $_ } }, >> @{"Name"="Integer"; >> "Expression"= { [int] $_ } }, >> @{"Name"="Hexadecimal"; >> "Expression"= { [Convert]::ToString([int] $_, 16) } }, >> @{"Name"="Binary"; >> "Expression"= { [Convert]::ToString([int] $_, 2) } } | >> FormatTable auto >>
Example 64. Integer, hexadecimal, and binary representations of possible file attributes (continued)
Property Integer Hexadecimal Binary
ReadOnly
1 1
1
Hidden
2 2
10
System
4 4
100
Directory
16 10
10000
Archive
32 20
100000
Device
64 40
1000000
Normal
128 80
10000000
Temporary
256 100
100000000
SparseFile
512 200
1000000000
ReparsePoint
1024 400
10000000000
Compressed
2048 800
100000000000
Offline
4096 1000
1000000000000
NotContentIndexed
8192 2000
10000000000000
Encrypted
16384 4000
100000000000000
Knowing how that 16417 number was formed, you can now use the properties in meaningful ways. For example, PowerShell’s –band operator allows you to check if a certain bit has been set:
PS >$encrypted = 16384 PS >$attributes = (GetItem test.txt force).Attributes PS >($attributes band $encrypted) –eq $encrypted True PS >$compressed = 2048 PS >($attributes band $compressed) –eq $compressed False PS >
Although the example above uses the numeric values explicitly, it would be more common to enter the number by its name:
PS >$archive = [System.IO.FileAttributes] "Archive" PS >($attributes band $archive) –eq $archive True
Simplify Math with Administrative Constants in Windows PowerShell
Problem
You want to work with common administrative numbers (that is, kilobytes, megabytes, and gigabytes) without having to remember or calculate those numbers.
Solution
Use PowerShell’s administrative constants (KB, MB, and GB) to help work with these common numbers.
Calculate the download time (in seconds) of a 10.18 megabyte file over a connection that gets 215 kilobytes per second:
PS >10.18mb / 215kb 48.4852093023256
Discussion
PowerShell’s administrative constants are based on powers of two, since those are the kind most commonly used when working with computers. Each is 1,024 times bigger than the one before it:
1kb = 1024 1mb = 1024 * 1 kb 1gb = 1024 * 1 mb
Some (such as hard drive manufacturers) prefer to call numbers based on powers of two “kibibytes,” “mebibytes,” and “gibibytes.” They use the terms “kilobytes,” “megabytes,” and “gigabytes” to mean numbers that are 1,000 times bigger than the one before it—numbers based on powers of 10.
Although not represented by administrative constants, PowerShell still makes it easy to work with these numbers in powers of 10—for example, to figure out how big a “300 GB” hard drive is when reported by Windows:
PS >$kilobyte = [Math]::Pow(10,3) PS >$kilobyte 1000 PS >$megabyte = [Math]::Pow(10,6) PS >$megabyte 1000000 PS >$gigabyte = [Math]::Pow(10,9) PS >$gigabyte 1000000000 PS >(300 * $gigabyte) / 1GB 279.396772384644