# Thursday, March 27, 2008

PowerShell has several "type accelerators" which are used exactly like a casting operation. Examples of these special operators are [xml] and [wmi]. The former is used for quickly converting a string of xml into a fully-fledged System.Xml.XmlDocument object.

Often I find myself converting things to and from hexadecimal using the -f operator, but this always seemed like just a little too much typing for me. Enter the [hex] accelerator type:

image

As you can see from the source below, there's no magic here. This is just a straight cast, but I have no namespace. If I had a namespace, say, like "Nivot.PowerShell", we'd have to cast using [nivot.powershell.hex] instead of just [hex]. All of the trickery is done using operator overloads in C#. These tells .NET (and in turn, powershell) how to behave should someone try to add, subtract, remove or divide our instances.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. public class Hex  
  6. {  
  7.     private readonly int _value = 0;  
  8.  
  9.     private Hex(int value)  
  10.     {  
  11.         _value = value;  
  12.     }  
  13.  
  14.     private Hex(string value)  
  15.     {  
  16.         _value = Convert.ToInt32(value, 16);  
  17.     }  
  18.  
  19.     public static implicit operator Hex(int value)  
  20.     {  
  21.         return new Hex(value);  
  22.     }  
  23.  
  24.     public static implicit operator int(Hex value)  
  25.     {  
  26.         return value._value;  
  27.     }  
  28.  
  29.     public static explicit operator Hex(string value)  
  30.     {  
  31.         return new Hex(value);  
  32.     }  
  33.  
  34.     public static Hex operator +(Hex op1, Hex op2)  
  35.     {  
  36.         return new Hex(op1._value + op2._value);  
  37.     }  
  38.  
  39.     public static Hex operator -(Hex op1, Hex op2)  
  40.     {  
  41.         return new Hex(op1._value - op2._value);  
  42.     }  
  43.  
  44.     public static Hex operator *(Hex op1, Hex op2)  
  45.     {  
  46.         return new Hex(op1._value * op2._value);  
  47.     }  
  48.  
  49.     public static Hex operator /(Hex op1, Hex op2)  
  50.     {  
  51.         return new Hex(op1._value / op2._value);  
  52.     }  
  53.  
  54.     public override string ToString()  
  55.     {  
  56.         return "0x" + _value.ToString("X");
  57.     }  

The next step is to tell PowerShell's formatter what to do with the new type. Here's a simple format definition that tells the formatter to call ToString() on the Hex instance. This is the method that does the conversion by calling ToString("X") on the integer field. "X" means format the integer as hexadecimal using upper case. A lower-case "x" would output the value using lower-case (if you couldn't guess ;-)).

  1. <Configuration> 
  2.   <ViewDefinitions> 
  3.     <View> 
  4.       <Name>Hex</Name> 
  5.       <ViewSelectedBy> 
  6.         <TypeName>Hex</TypeName> 
  7.       </ViewSelectedBy> 
  8.       <CustomControl> 
  9.         <CustomEntries> 
  10.           <CustomEntry> 
  11.             <CustomItem> 
  12.               <ExpressionBinding> 
  13.                 <ScriptBlock>$_.ToString()</ScriptBlock> 
  14.               </ExpressionBinding> 
  15.             </CustomItem> 
  16.           </CustomEntry> 
  17.         </CustomEntries> 
  18.       </CustomControl> 
  19.     </View> 
  20.   </ViewDefinitions> 
  21. </Configuration> 

If we don't load this format file, PowerShell just emits a couple of blank lines when you try to use it.  You'll notice from the screenshot above that the blank lines still appear. I'm not sure how to remove these - it looks pretty ugly compared to the output of the [int] object. If you want to play with this, you can download the zip file below and unzip the contents into a single folder and run "hex.ps1". I didn't bother with a full Snap-In, it just loads the DLL using reflection. It also loads the format ps1xml too. Have fun.

HexAccelerator1.zip (2.4 KB)
posted on Thursday, March 27, 2008 9:19:15 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
Related posts:
PowerShell v2.0 – Differences Between CTP3/Win7Beta and Win7RC
Pscx 1.2 Beta Released
PowerShell 2.0 CTP3: Modules, in Practice - Closures
PowerShell 2.0 CTP3: Modules, in Practice – .NET Interfaces
PowerShell 2.0 CTP3: Modules, in Practice.
Visual Studio 2008 Extensions for WSS 3.0 v1.3 March CTP

Referred by:
http://www.codeplex.com/PowerShellCX/Thread/View.aspx?Thread... [Referral]
powershell type accelerators (www.google.com) [Referral]
powershell hex (www.google.be) [Referral]
powershell custom type (www.google.fr) [Referral]
powershell cast hex (www.google.com) [Referral]
[XML] type cast in powershell (www.google.com) [Referral]
http://delicious.com/PowerShellJedi/?page=5 [Referral]
powershell (search.live.com) [Referral]
powershell cast (www.google.co.kr) [Referral]
powershell cast (www.google.com) [Referral]
how to put blank line in custom object in powershell (www.google.com.au) [Referral]
powershell override toString (www.google.com) [Referral]
http://www.codeplex.com/PowerShellCX/Thread/View.aspx?Thread... [Referral]
create custom types in powershell (search.live.com) [Referral]
type casting an object to string powershell (www.google.com) [Referral]
make your own Accelerator (www.google.co.nz) [Referral]
making your own accelerator is the xml file (www.google.com) [Referral]
powershell custom object formatting (www.google.co.uk) [Referral]
powershell custom types (www.google.nl) [Referral]
"powershell" creating type (www.google.de) [Referral]
powershell [hex] (www.google.de) [Referral]
powershell .net namespace type name (www.google.com) [Referral]
powershell create type (www.google.com) [Referral]
powershell override ToString (www.google.se) [Referral]
typecasting in powershell (www.google.se) [Referral]
powershell custom types (www.google.com) [Referral]
customType filed + share point (www.google.co.in) [Referral]
make your own IE accelerators (www.google.com) [Referral]
create type powershell (www.google.com) [Referral]
powershell type accelerators (www.google.com) [Referral]
http://images.google.com/imgres?imgurl=http://www.nivot.org/... [Referral]
powershell type cast (search.live.com) [Referral]
powershell type cast (www.google.co.uk) [Referral]
powershell new-type (www.google.com) [Referral]
powershell custom class (search.live.com) [Referral]
make your own accelerator ie (www.google.com) [Referral]
type cast powershell (search.live.com) [Referral]
powershell create custom class (www.google.nl) [Referral]
Create Your Own Accelerators (www.google.co.in) [Referral]
type cast operator in powershell (www.google.ru) [Referral]
powershell override tostring method "keith hill" (search.live.com) [Referral]
powershell create custom object (www.google.nl) [Referral]
powershell create custom type (www.google.com.tr) [Referral]
+powershell +"upper case" +"lower case" (www.google.com) [Referral]
Powershell Custom Objects (www.google.com) [Referral]
Powershell String .toString (www.google.cz) [Referral]
powershell remove empty lines (www.google.co.uk) [Referral]
How to typecastany object in string in powershell (www.google.co.in) [Referral]
"{0}" lower case format powershell (www.google.com) [Referral]
create your own ie accelerator (www.google.com) [Referral]
powershell lower (search.live.com) [Referral]
accelerators+types (www.google.com.eg) [Referral]
powershell [xml] cast (www.google.com) [Referral]
type accelerators for powershell (www.google.co.in) [Referral]
create type powershell (www.google.fr) [Referral]
powershell custom objects (www.google.com) [Referral]
powershell operator overloading (www.google.com) [Referral]
powershell Format-Custom (www.google.co.jp) [Referral]
powershell lower case (www.google.ch) [Referral]
powershell remove blank spaces from string (www.google.ca) [Referral]
powershell type accelerator (www.google.be) [Referral]
powershell custom types (www.google.de) [Referral]
create own type powershell (www.google.ch) [Referral]
Accelerators add your own (www.google.com) [Referral]
powershell tostring() (www.google.pt) [Referral]
powershell tostring (www.google.co.uk) [Referral]
powershell custom types (www.google.ca) [Referral]
custom ie accelerator (www.google.com) [Referral]
powershell custom objects (www.google.dk) [Referral]
powershell create thread (www.google.com) [Referral]
powershell xml type accelerator (www.google.com) [Referral]
Create Your Own Accelerator on website (www.google.com) [Referral]
powershell type override (www.google.co.uk) [Referral]
typecasting in powershell (www.google.com) [Referral]
create custom accellerators (www.google.com) [Referral]
powershell remove empty line (www.google.com) [Referral]
powershell hex (www.google.com) [Referral]
powershell typecast (www.google.de) [Referral]
powershell type accelerators (www.google.com.au) [Referral]
powershell custom operators (www.google.com) [Referral]
powershell custom object type (www.google.com) [Referral]
Create a PowerShell Class (search.live.com) [Referral]
powershell type accelerator (www.google.ca) [Referral]
powershell create type definition (www.google.de) [Referral]
creating Powershell Custom Class (www.google.com) [Referral]
creating new powershell types (www.google.com) [Referral]
powershell type conversion int string (www.google.nl) [Referral]
powershell format-custom (www.google.nl) [Referral]
powershell remove empty lines (www.google.de) [Referral]
powershell cast (www.google.es) [Referral]
powershell create custom object in C# (www.kumo.com) [Referral]
http://social.msdn.microsoft.com/Search/en-US/?query=powersh... [Referral]
powershell format-custom (www.google.co.uk) [Referral]
powershell XML cast as integer (www.google.com) [Referral]
powershell tostring (www.google.es) [Referral]
powershell custom int (www.google.com) [Referral]
powershell find empty string (www.google.fr) [Referral]
powershell create type (www.google.com) [Referral]
define accelerator in casting (www.google.com) [Referral]
powershell custom view (www.google.com) [Referral]
create your own accelerators (www.google.co.uk) [Referral]
typecast object powershell (www.google.com) [Referral]
powershell remove blank sign (www.google.se) [Referral]
powershell cast (www.kumo.com) [Referral]
how to create own accelerator (www.google.cz) [Referral]
http://www.techtalkz.com/microsoft-windows-powershell/470031... [Referral]
powershell format-hex (www.google.com) [Referral]
how to add custom accelerator to google (www.google.com) [Referral]
PowerShell "-f" Format Operator (www.google.co.uk) [Referral]
powershell create object (www.google.com) [Referral]
powershell custom types (www.google.de) [Referral]
"Format-Custom" powershell (www.google.co.jp) [Referral]
powershell hex values (www.google.com) [Referral]
create type powershell (www.google.com.br) [Referral]
powershell casting (www.google.com) [Referral]
create custom accelerators (www.google.es) [Referral]
powershell string to lower case (www.google.com) [Referral]
new-type powershell (www.google.com) [Referral]
powershell cast (www.google.com) [Referral]
powershell type accelerator create (www.google.ch) [Referral]
powershell cast object to string (www.google.co.uk) [Referral]
Powershell cast type (www.google.com) [Referral]
custom class powershell (www.google.com) [Referral]
ie accelerator xml folder (www.google.de) [Referral]
http://images.google.com/imgres?imgurl=http://www.nivot.org/... [Referral]
http://images.google.com/imgres?imgurl=http://www.nivot.org/... [Referral]
powershell load use custom c# type (www.google.com) [Referral]
powershell format int (www.google.com) [Referral]
powershell typecast COM (www.google.be) [Referral]
powershell cast (www.google.co.uk) [Referral]
http://us.mg2.mail.yahoo.com/dc/blank.html?bn=1277.35&.intl=... [Referral]
create your own type (www.google.com) [Referral]
Add " to string powershell (www.google.ch) [Referral]
creating a custom accelerator, problem with spaces (www.google.com) [Referral]
powershell add hex (www.google.com) [Referral]
http://laurent-dardenne.developpez.com/articles/Windows/Powe... [Referral]
c# operatoren für hexa* (www.google.de) [Referral]
ie accelerator google nz (www.google.co.nz) [Referral]
images.google.co.kr / referral (www.google.com) [Referral]
powershell add-type remove (www.google.be) [Referral]
powershell object type (www.google.com) [Referral]
powershell cast string (www.google.com) [Referral]
powershell static fields from custom snapins (www.google.com) [Referral]
create custom iexplorer accelerator (www.google.com) [Referral]
powershell uppercase to lowercase (www.google.be) [Referral]
PowerShell type accelerator (www.google.com) [Referral]
powershell cast string (www.google.be) [Referral]
powershell types accelerators v1 (search.live.com) [Referral]
c# create custom type (www.google.com) [Referral]
PowerShell custom class (www.google.com) [Referral]
powershell CustomClass (www.google.com) [Referral]
powershell string to integer (www.google.se) [Referral]
Accelerator create in c# + windows mobile application (www.google.com) [Referral]
powershell hex type (www.google.com) [Referral]
powershell hex output value (www.google.de) [Referral]
make your own accelerator (uk.search.yahoo.com) [Referral]
hex bits powershell (www.google.com) [Referral]
create type powershell (search.live.com) [Referral]
powershell c# using full type name (search.live.com) [Referral]
operator "using" in Powershell (www.google.com) [Referral]
powershell typecast (www.google.com) [Referral]
convert type object to type string in powershell (www.google.com) [Referral]
powershell "format operator" -book (www.google.com) [Referral]
make custom accelerators (www.google.be) [Referral]
powershell string to hex convert (www.google.ca) [Referral]
lowercase powershell (www.google.com) [Referral]
powershell convert object to string (www.google.hu) [Referral]
powershell cast (www.kumo.com) [Referral]
c# create a custom type from a string (www.google.com.mt) [Referral]
tostring powershell (www.google.com) [Referral]
powershell tostring (www.google.com) [Referral]
create custom ie accelerator (www.google.ca) [Referral]
hexadecimal to string in powershell script (www.google.co.in) [Referral]
powershell convert integer to string (www.google.co.uk) [Referral]
casting powershell (www.google.de) [Referral]
powershell custom type (www.google.com) [Referral]
powershell create own object (www.google.nl) [Referral]
powershell customclass (www.google.com.au) [Referral]
powershell string operators (www.kumo.com) [Referral]
powershell cast (www.google.com) [Referral]
xml powershell type="integer" (www.google.com) [Referral]
iexplore accelerators location (www.google.co.uk) [Referral]
powershell remove empty spaces (www.google.com) [Referral]
powershell format hex (search.live.com) [Referral]
powershell convert int to string (www.google.com) [Referral]
http://laurent-dardenne.developpez.com/articles/Windows/Powe... [Referral]
powershell string formatting integer (www.google.de) [Referral]
powershell convert lowercase (www.google.co.uk) [Referral]
powershell comparison custom class overload C# (www.google.com) [Referral]
powershell remove blank lines (www.google.com) [Referral]
live.com accelerators xml (www.google.com) [Referral]
powershell convert integer (www.google.com) [Referral]
PowerShell.Create() (www.google.com) [Referral]
toString powershell uppercase (www.google.com) [Referral]
convert text to xml powershell (www.google.com) [Referral]
c# createtype powershell (www.google.co.uk) [Referral]
custom format file powershell (www.google.com) [Referral]
create own search accelerator (www.google.be) [Referral]
casting objects in powershell (www.kumo.com) [Referral]
powershell xml (search.live.com) [Referral]
cast in powershell (www.google.fr) [Referral]
typecast powershell objects (www.google.co.in) [Referral]
how to add custom accelerator (www.google.co.in) [Referral]
powershell remove empty lines (www.google.de) [Referral]
powershell remove blank lines (www.google.de) [Referral]
create ie accelerator (search.yahoo.com) [Referral]
create an object in powershell (www.bing.com) [Referral]
powershell delete empty lines (www.google.com) [Referral]
powershell uppercase (www.google.com) [Referral]
powershell remove empty lines (www.google.com) [Referral]
powershell int convert string (search.live.com) [Referral]
http://pscx.codeplex.com/Thread/View.aspx?ThreadId=24606 [Referral]
powershell create custom page html (www.google.com.br) [Referral]
add custom class powershell (www.google.co.in) [Referral]
powershell + type casting (www.bing.com) [Referral]
powershell tricks static type (www.google.com) [Referral]
specify google nz in ie accelerator (www.google.co.nz) [Referral]
"PowerShell -f format operator" (www.google.com) [Referral]
powershell string to hex (www.google.cz) [Referral]
http://laurent-dardenne.developpez.com/articles/Windows/Powe... [Referral]
Powershell text to hexa (www.google.fr) [Referral]
powershell converting objects into xml (www.bing.com) [Referral]
http://adipex.idoo.com/doessnor49/ [Referral]
powershell casting com objects (www.google.com) [Referral]
powershell new-object or typecast (www.google.com) [Referral]
hexadecimal powershell (www.google.co.nz) [Referral]
method removeemptyline powershell (www.google.com) [Referral]
laurent dardenne powershell (www.google.fr) [Referral]
powershell remove empty lines (www.google.ch) [Referral]
PowerShell custom types type.ps1xml (www.google.com) [Referral]
powershell remove empty spaces (www.google.com) [Referral]
remove blank space with powershell (www.google.fr) [Referral]
type accelerator powershell (www.google.cz) [Referral]
-eq lowercase powershell (www.google.it) [Referral]
date hex powershell (www.google.com) [Referral]
convert powershell string to object (www.bing.com) [Referral]
create my own accelerator (www.bing.com) [Referral]
powershell tostring (www.google.com) [Referral]
convert reflection.emit "custom type" (www.google.nl) [Referral]
powershell typecast (www.google.de) [Referral]
powershell convert custom object int (www.google.de) [Referral]
powershell format integer (www.google.co.uk) [Referral]
powershell string upper case (www.google.nl) [Referral]
powershell create object (www.bing.com) [Referral]
Powershell create type (www.google.com) [Referral]
powershell cast return types (www.google.de) [Referral]
powershell convert hexa string to text (www.google.fr) [Referral]
powershell thread namespace create (www.google.com) [Referral]
Powershell format <CustomItem> (www.google.com) [Referral]
powershell integer to string (www.google.de) [Referral]
powershell tostring upper (www.google.ch) [Referral]
powershell custom class (www.bing.com) [Referral]
powershell cast string as hex (www.google.com) [Referral]
powershell type accelerators (www.google.com) [Referral]
create object from text powershell (www.google.com) [Referral]
powershell lowercase (www.google.nl) [Referral]
powershell ucase (www.google.de) [Referral]
powershell format int to string (www.google.com.br) [Referral]
text to xml converter powershell (www.google.se) [Referral]
disable live search accelerator (www.google.com) [Referral]
powershell convert object to string (www.google.ca) [Referral]
powershell cast to int (www.google.de) [Referral]
cast powershell (www.google.pt) [Referral]
http://www.nigma.ru/index.php?s=override&t=web&pq=Powershell... [Referral]
powershell convert int (www.google.fr) [Referral]
casting in powershell (www.bing.com) [Referral]
powershell "-f format operator" (www.google.fr) [Referral]
powershell convert object to string (www.google.com) [Referral]
+create +"static type" +C# (www.google.com.br) [Referral]
convert text to lowercase in sharepoint designer (www.google.com) [Referral]
how to use C# thread classes in powershell (www.google.com) [Referral]
how to make a blank line in powershell (www.google.co.uk) [Referral]
powershell convert int to string (www.google.com) [Referral]
powershell convert string to int (www.bing.com) [Referral]
powershell cast to integer (www.google.com) [Referral]
powershell lowercase (www.google.com) [Referral]
powershell remove text in string (www.bing.com) [Referral]
powershell lowcase (www.google.com.br) [Referral]
Comments are closed.