Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Convert PowerShell Object to Hashtable Revised

Posted on January 22, 2013September 22, 2014

squarepatternA while back I posted an advanced PowerShell function that would take an object and convert it to a hashtable. The premise was simple enough: look at the incoming object with Get-Member to discover the property names then create a hashtable with each property name as a hashtable key. I've a need to use this over the last few weeks and realized the function could use a little updating.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The new version works like the old, with the addition of one new parameter. I decided I wanted a way to exclude properties from the hashtable. Of course this presumes you know in advance about the object you are working with and what properties you wish to exclude. This is the core segment of code, which has also been revised over version 1.

#go through the list of names and add each property and value to the hash table
$names | ForEach-Object {
 #only add properties that haven't been excluded
 if ($Exclude -notcontains $_) {
   #only add if -NoEmpty is not called and property has a value
   if ($NoEmpty -AND -Not ($inputobject.$_)) {
     Write-Verbose "Skipping $_ as empty"
   }
 else {
   Write-Verbose "Adding property $_"
   $hash.Add($_,$inputobject.$_)
 }
} #if exclude notcontains
else {
  Write-Verbose "Excluding $_"
}
} #foreach

In the first version I was duplicating some code and I generally try to avoid that. In this snippet $names is the collection of property names and $Inputobject is the piped in object. Armed with this tool I can create hashtables from objects with just the properties I want.

PS Scripts:\> $h = get-service spooler | ConvertTo-HashTable -NoEmpty -Exclude CanStop,CanPauseandContinue
PS Scripts:\> $h

Name Value
---- -----
ServiceName spooler
ServiceType Win32OwnProcess, InteractiveProcess
Name spooler
DisplayName Print Spooler
MachineName .
Status Running
ServiceHandle SafeServiceHandle
DependentServices {Fax}
RequiredServices {http, RPCSS}
ServicesDependedOn {http, RPCSS}

This version should work in PowerShell 2.0 or 3.0. Download ConvertTo-Hashtable2.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

6 thoughts on “Convert PowerShell Object to Hashtable Revised”

  1. Claudio Spizzi says:
    January 22, 2013 at 11:26 am

    This is a great function that I use very often. But I have change it additionally with a $Inclue parameter.

    How it works:
    If the [String[]] $Include Count ist equals to 0, then use the Get-Member function to get the member name, it its greather than 0, us this assignment: $names = $Include.

    Greetings,
    Claudio

  2. Martin9700 says:
    January 23, 2013 at 6:50 am

    Could use a link to your old article! I’m curious why you would do this?

    1. Jeffery Hicks says:
      January 23, 2013 at 7:50 am

      I might want to take the object and transform it. Using a hashtable and modifying it is pretty easy. The hashtable can then be turned back into an object. Or I might want to combine objects. Much easier to join a few hash tables and then turn that back into an object. Or I might want a hash table of properties that I can splat later against some other cmdlet.

      1. Martin9700 says:
        January 23, 2013 at 9:22 am

        Thanks Jeff!

  3. Pingback: Join PowerShell Hash Tables | The Lonely Administrator
  4. Windows PowerShell 2.0 Workshop says:
    April 12, 2013 at 6:17 am

    Thanks a lot for this useful instruction.
    I like to work with Powershell and your Blog is a good source to learn more about it.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d