Trevor Sullivan's Tech Room

Minding the gap between administration and development

PowerShell: Shortening Web Service Type Names with a Hashtable

Posted by Trevor Sullivan on 2011/07/13

When you use the New-WebServiceProxy class, you probably have noticed that PowerShell dynamically generates some really ugly type names. For example, if we get a reference to the Bing web service (you’ll need to get an API key first):

$BingSearch = New-WebServiceProxy -Class BingSearch -Uri "$ApiKey"

… and examine the types contained within it:

$BingSearch.GetType().Assembly.GetExportedTypes() | select FullName

… you’ll notice some ridiculously long type names based on your API key, such as:


Whenever you want to instantiate one of these types, do you want to be typing that enormous type name? Probably not — so, what can we do to make this easier? A simple solution here would be to build a hashtable of short type names that map to their long type names. That way, whenever we want to instantiate a type, all we have to remember is our hashtable variable, and the short name of the type we want.

Let’s take a look:

# Create a hashtable to hold our list of TypeName / FullName mappings
$WebServiceTypes = @{}
# Iterate over each type contained in the BingSearch dynamic assembly
foreach ($Type in $BingSearch.GetType().Assembly.GetExportedTypes())
    # Add an item to our hashtable with the short type name and full type name
    $WebServiceTypes.Add($Type.Name, $Type.FullName);

# Instantiate a SearchRequest (which is really a:
# Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyXXXXXXXXXXXXXXXXXXXXXXXXXXX.SearchRequest
New-Object $WebServiceTypes.SearchRequest

That’s all for now folks – hope this helps!


2 Responses to “PowerShell: Shortening Web Service Type Names with a Hashtable”

  1. Keep up the good blogging guys!

  2. Nice trick. New-WebServiceProxy also has a -Namespace parameter which allows you to specify a namespace rather than using the generated namespace.

    $BingSearch = New-WebServiceProxy -Namespace BS -Class BingSearch -Uri “$ApiKey”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: