Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Last active May 30, 2018 10:34
Show Gist options
  • Select an option

  • Save JohnLBevan/eb4d80d33909901f8cb411801f4005f2 to your computer and use it in GitHub Desktop.

Select an option

Save JohnLBevan/eb4d80d33909901f8cb411801f4005f2 to your computer and use it in GitHub Desktop.
List the properties of 2 objects side by side for comparison
function Compare-ObjectProperties {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
$InputObject
,
[Parameter(Mandatory = $true)]
$CompareObject
)
Process {
$properties = @($InputObject, $CompareObject) | Get-Member -MemberType NoteProperty, Property | Select-Object -ExpandProperty Name | Sort-Object -Unique
$properties | %{
$result = [pscustomobject][ordered]@{
PropertyName = $_
InputObject = $InputObject."$_"
CompareObject = $CompareObject."$_"
}
$result.PSObject.TypeNames.Insert(0,'JohnLBevan.CompareObjectPropertiesResult')
$result
}
}
}
$a = Get-ADUser someone
$b = Get-ADUser sometwo
clear-host
#without format definition file
#Compare-ObjectProperties $a $b | ?{$_.InputObject -eq $_.CompareObject} | ft @{Expression='PropertyName'; Width=20;}, @{Expression='InputObject'; Width=100; Align="right";}, @{Expression='CompareObject'; Width=100; Align="left";}
#using format defintion files per: https://stackoverflow.com/questions/50600391/is-it-possible-to-define-display-data-on-an-objects-properties
Update-FormatData -AppendPath '.\JohnLBevan.CompareObjectPropertiesResult.Format.ps1xml'
Compare-ObjectProperties $a $b | ?{$_.InputObject -eq $_.CompareObject}
<# Sample Output
PropertyName InputObject CompareObject
------------ ----------- -------------
AccountExpiration...
AccountLockoutTime
AccountNotDelegated False False
AllowReversiblePa... False False
BadLogonCount 0 0
badPwdCount 0 0
c GB GB
CannotChangePassword False False
co United Kingdom United Kingdom
codePage 0 0
#>
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Default</Name>
<ViewSelectedBy>
<TypeName>JohnLBevan.CompareObjectPropertiesResult</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>20</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Width>90<!--($host.UI.RawUI.BufferSize.Width - 18) / 2--></Width>
<Alignment>right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Width>90</Width>
<Alignment>left</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>PropertyName</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>InputObject</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>CompareObject</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment