Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell : Display a GUID as string by default

When you have a GUID object in PowerShell by default this gives no output, only some empty lines.

See output below :

PS C:\> [GUID]$a = "00000000-0000-0000-0000-000000000000"                                                               
PS C:\> $a                                                                                                              
                                                                                                                        
                                                                                                                        
                                                                                                                        
PS C:\> $a.ToString()                                                                                                   
00000000-0000-0000-0000-000000000000                                                                                    
PS C:\> "$a"                                                                                                            
00000000-0000-0000-0000-000000000000                                                                                    
PS C:\> Update-TypeData C:\MowTemp\GUID.ps1xml                                                                          
PS C:\> $a                                                                                                              
                                                                                                                        
GUID                                                                                                                    
----                                                                                                                    
00000000-0000-0000-0000-000000000000                                                                                    
                                                                                                                        
                                                                                                                        

The reason for this is that a GUID has no properties only 2 Methods to show the value of the GUID

ToByteArray()
ToString()

one option is Embedding it in a string "$a" 

But as it is handy to show the GUI as string by default I created the following TypeData file that adds a GUID property to the GUID object so that the string value is Shown by default in PowerShell as in the second example above after the TypeData file is loaded

the TypeData file looks like this :

<?xml version="1.0" encoding="utf-8" ?> 
<Types> 
    <Type> 
        <Name>System.Guid</Name> 
        <Members> 
            <ScriptProperty>
                <Name>GUID</Name>
                 <GetScriptBlock>
                    $this.Tostring()
                </GetScriptBlock>
            </ScriptProperty> 
        </Members> 
    </Type> 
</Types>

Democracy to the Types ! 

Enjoy,

Greetings MOW

Published Friday, July 24, 2009 11:47 AM by MoW
Filed under: ,

Comments

# re: PowerShell : Display a GUID as string by default

Looks like this is already in PowerShell v2.  Joy.

Friday, July 24, 2009 5:07 PM by
Anonymous comments are disabled