Skip to content

Commit d5cf9b1

Browse files
davidsmatlaksdwheeler
authored andcommitted
issue 4543 (#4799)
1 parent 03c326a commit d5cf9b1

File tree

6 files changed

+738
-277
lines changed

6 files changed

+738
-277
lines changed

reference/3.0/Microsoft.PowerShell.Utility/Compare-Object.md

Lines changed: 121 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,144 @@
11
---
2-
ms.date: 06/09/2017
2+
ms.date: 09/17/2019
33
schema: 2.0.0
44
locale: en-us
55
keywords: powershell,cmdlet
66
online version: https://go.microsoft.com/fwlink/?linkid=113286
77
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
88
title: Compare-Object
99
---
10+
1011
# Compare-Object
1112

1213
## SYNOPSIS
13-
1414
Compares two sets of objects.
1515

1616
## SYNTAX
1717

18+
### All
19+
1820
```
19-
Compare-Object [-ReferenceObject] <PSObject[]> [-DifferenceObject] <PSObject[]> [-SyncWindow <Int32>]
20-
[-Property <Object[]>] [-ExcludeDifferent] [-IncludeEqual] [-PassThru] [-Culture <String>] [-CaseSensitive]
21-
[<CommonParameters>]
21+
Compare-Object [-ReferenceObject] <PSObject[]> [-DifferenceObject] <PSObject[]>
22+
[-SyncWindow <Int32>] [-Property <Object[]>] [-ExcludeDifferent] [-IncludeEqual] [-PassThru]
23+
[-Culture <String>] [-CaseSensitive] [<CommonParameters>]
2224
```
2325

2426
## DESCRIPTION
2527

26-
The `Compare-Object` cmdlet compares two sets of objects.
27-
One set of objects is the "reference set," and the other set is the "difference set."
28+
The `Compare-Object` cmdlet compares two sets of objects. One set of objects is the **reference**,
29+
and the other set of objects is the **difference**.
2830

29-
The result of the comparison indicates whether a property value appeared only in the object from the reference set (indicated by the \<= symbol), only in the object from the difference set (indicated by the `=>` symbol) or, if the `IncludeEqual` parameter is specified, in both objects (indicated by the `==` symbol).
31+
The result of the comparison indicates whether a property value appeared only in the **reference**
32+
object (`<=`) or only in the **difference** object (`=>`). If the **IncludeEqual** parameter is
33+
used, (`==`) indicates the value is in both objects.
3034

31-
If the reference set or the difference set is null ($null), this cmdlet generates a terminating error.
35+
If the **reference** or the **difference** objects are null (`$null`), `Compare-Object` generates a
36+
terminating error.
37+
38+
Some examples use splatting to reduce the line length of the code samples. For more information, see
39+
[about_Splatting](../Microsoft.PowerShell.Core/About/about_Splatting.md). And, the examples use two
40+
text files, with each value on a separate line. `Textfile1.txt` contains the values: dog, squirrel,
41+
and bird. `Textfile1.txt` contains the values: cat, bird, and racoon.
3242

3343
## EXAMPLES
3444

3545
### Example 1: Compare the content of two text files
3646

47+
This example compares the contents of two text files. The output displays only the lines that are
48+
different between the files. `Textfile1.txt` is the **reference** object (`<=`) and
49+
`Textfile2.txt`is the **difference** object (`=>`).
50+
51+
Lines with content that appear in both files aren't displayed.
52+
3753
```powershell
38-
Compare-Object -ReferenceObject $(Get-Content C:\test\testfile1.txt) -DifferenceObject $(Get-Content C:\test\testfile2.txt)
54+
Compare-Object -ReferenceObject $(Get-Content -Path C:\Test\Testfile1.txt) -DifferenceObject $(Get-Content -Path C:\Test\Testfile2.txt)
3955
```
4056

41-
This command compares the contents of two text files.
42-
It displays only the lines that appear in one file or in the other file, not lines that appear in both files.
57+
```Output
58+
InputObject SideIndicator
59+
----------- -------------
60+
cat =>
61+
racoon =>
62+
dog <=
63+
squirrel <=
64+
```
4365

4466
### Example 2: Compare each line of content in two text files
4567

68+
This example uses the **IncludeEqual** to compare each line of content in two text files. All the
69+
lines of content from both files are displayed.
70+
71+
The **SideIndicator** specifies if the line appears in the `Textfile1.txt` **reference** object
72+
(`<=`), `Textfile2.txt` **difference** object (`=>`), or both files (`==`).
73+
74+
```powershell
75+
$objects = @{
76+
ReferenceObject = $(Get-Content -Path C:\Test\Testfile1.txt)
77+
DifferenceObject = $(Get-Content -Path C:\Test\Testfile2.txt)
78+
}
79+
Compare-Object @objects -IncludeEqual
80+
```
81+
82+
```Output
83+
InputObject SideIndicator
84+
----------- -------------
85+
bird ==
86+
cat =>
87+
racoon =>
88+
dog <=
89+
squirrel <=
90+
```
91+
92+
### Example 3: Compare each line of content and exclude the differences
93+
94+
This example uses the **IncludeEqual** and **ExcludeDifferent** parameters to compare each line of
95+
content in two text files.
96+
97+
Because the command uses the **ExcludeDifferent** parameter, the output only contains lines
98+
contained in both files, as shown by the **SideIndicator** (`==`).
99+
46100
```powershell
47-
Compare-Object -ReferenceObject $(Get-Content C:\Test\testfile1.txt) -DifferenceObject $(Get-Content C:\Test\testfile2.txt) -IncludeEqual
101+
$objects = @{
102+
ReferenceObject = $(Get-Content -Path C:\Test\Testfile1.txt)
103+
DifferenceObject = $(Get-Content -Path C:\Test\Testfile2.txt)
104+
}
105+
Compare-Object @objects -IncludeEqual -ExcludeDifferent
106+
```
107+
108+
```Output
109+
InputObject SideIndicator
110+
----------- -------------
111+
bird ==
48112
```
49113

50-
This command compares each line of content in two text files.
51-
It displays all lines of content from both files, indicating whether each line appears in only Textfile1.txt or Textfile2.txt or whether each line appears in both files.
114+
### Example 4: Compare two sets of process objects
52115

53-
### Example 3: Compare two sets of process objects
116+
This example compares two sets of objects that contain the computer's running processes.
54117

55118
```powershell
56119
$Processes_Before = Get-Process
57-
notepad
120+
notepad.exe
58121
$Processes_After = Get-Process
59122
Compare-Object -ReferenceObject $Processes_Before -DifferenceObject $Processes_After
60123
```
61124

62-
```output
63-
InputObject SideIndicator
64-
----------- -------------
65-
System.Diagnostics.Process (notepad) =>
125+
```Output
126+
InputObject SideIndicator
127+
----------- -------------
128+
System.Diagnostics.Process (notepad) =>
66129
```
67130

68-
These commands compare two sets of process objects.
69-
70-
The first command uses the `Get-Process` cmdlet to get the processes on the computer.
71-
It stores them in the `$processes_before` variable.
131+
The `Get-Process` cmdlet gets the computer's running processes and stores them in the
132+
`$Processes_Before` variable.
72133

73-
The second command starts Notepad.
134+
The **notepad.exe** application is started.
74135

75-
The third command uses the `Get-Process` cmdlet again and stores the resulting processes in the `$processes_after` variable.
136+
`Get-Process` gets the computer's updated list of running processes and stores them in the
137+
`$Processes_After` variable.
76138

77-
The fourth command uses the `Compare-Object` cmdlet to compare the two sets of process objects.
78-
It displays the differences between them, which include the new instance of Notepad.
139+
The `Compare-Object` compare the two sets of process objects stored in the `$Processes_Before` and
140+
`$Processes_After` variables. The output displays the difference, **notepad.exe**, from the
141+
`$Processes_After` object.
79142

80143
## PARAMETERS
81144

@@ -113,23 +176,29 @@ Accept wildcard characters: False
113176
114177
### -DifferenceObject
115178
116-
Specifies the objects that are compared to the reference objects.
179+
Specifies the objects that are compared to the **reference** objects.
117180
118181
```yaml
119182
Type: PSObject[]
120183
Parameter Sets: (All)
121184
Aliases:
122185

123186
Required: True
124-
Position: 2
187+
Position: 1
125188
Default value: None
126189
Accept pipeline input: True (ByValue)
127190
Accept wildcard characters: False
128191
```
129192
130193
### -ExcludeDifferent
131194
132-
Indicates that this cmdlet displays only the characteristics of compared objects that are equal.
195+
Indicates that this cmdlet displays only the characteristics of compared objects that are equal. The
196+
differences between the objects are discarded.
197+
198+
Use **ExcludeDifferent** with **IncludeEqual** to display only the lines that match between the
199+
**reference** and **difference** objects.
200+
201+
If **ExcludeDifferent** is specified without **IncludeEqual**, there's no output.
133202
134203
```yaml
135204
Type: SwitchParameter
@@ -145,8 +214,10 @@ Accept wildcard characters: False
145214
146215
### -IncludeEqual
147216
148-
Indicates that this cmdlet displays characteristics of compared objects that are equal.
149-
By default, only characteristics that differ between the reference and difference objects are displayed.
217+
**IncludeEqual** displays the matches between the **reference** and **difference** objects.
218+
219+
By default, the output also includes the differences between the **reference** and **difference**
220+
objects.
150221
151222
```yaml
152223
Type: SwitchParameter
@@ -162,7 +233,8 @@ Accept wildcard characters: False
162233
163234
### -PassThru
164235
165-
When you use the **PassThru** parameter, `Compare-Object` omits the `PSCustomObject` wrapper around the compared objects and returns the differing objects, unchanged.
236+
When you use the **PassThru** parameter, `Compare-Object` omits the **PSCustomObject** wrapper
237+
around the compared objects and returns the differing objects, unchanged.
166238

167239
```yaml
168240
Type: SwitchParameter
@@ -178,7 +250,7 @@ Accept wildcard characters: False
178250

179251
### -Property
180252

181-
Specifies an array of properties of the reference and difference objects to compare.
253+
Specifies an array of properties of the **reference** and **difference** objects to compare.
182254

183255
```yaml
184256
Type: Object[]
@@ -210,9 +282,10 @@ Accept wildcard characters: False
210282

211283
### -SyncWindow
212284

213-
Specifies the number of adjacent objects that this cmdlet inspects while looking for a match in a collection of objects.
214-
This cmdlet examines adjacent objects when it does not find the object in the same position in a collection.
215-
The default value is `[Int32]::MaxValue`, which means that this cmdlet examines the entire object collection.
285+
Specifies the number of adjacent objects that `Compare-Object` inspects while looking for a match in
286+
a collection of objects. `Compare-Object` examines adjacent objects when it doesn't find the object
287+
in the same position in a collection. The default value is `[Int32]::MaxValue`, which means that
288+
`Compare-Object` examines the entire object collection.
216289

217290
```yaml
218291
Type: Int32
@@ -228,23 +301,28 @@ Accept wildcard characters: False
228301

229302
### CommonParameters
230303

231-
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
304+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
305+
-InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose,
306+
-WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
232307

233308
## INPUTS
234309

235310
### System.Management.Automation.PSObject
236311

237-
You can pipe a **DifferenceObject** object to this cmdlet.
312+
You can send an object down the pipeline to the **DifferenceObject** parameter.
238313

239314
## OUTPUTS
240315

241316
### None
242317

243-
If the objects are the same, nothing is returned.
318+
If the **reference** object and the **difference** object are the same, there's no output.
244319

245320
### System.Management.Automation.PSCustomObject
246321

247-
If the objects are different, `Compare-Object` wraps the differing objects in a `PSCustomObject` wrapper with a **SideIndicator** property to reference the differences. When you use the **PassThru** parameter, `Compare-Object` omits the `PSCustomObject` wrapper around the compared objects and returns the differing objects, unchanged.
322+
If the objects are different, `Compare-Object` wraps the differing objects in a `PSCustomObject`
323+
wrapper with a **SideIndicator** property to reference the differences. When you use the
324+
**PassThru** parameter, `Compare-Object` omits the `PSCustomObject` wrapper around the compared
325+
objects and returns the differing objects, unchanged.
248326

249327
## NOTES
250328

@@ -267,4 +345,3 @@ If the objects are different, `Compare-Object` wraps the differing objects in a
267345
[Where-Object](../Microsoft.PowerShell.Core/Where-Object.md)
268346

269347
[Get-Process](../Microsoft.PowerShell.Management/Get-Process.md)
270-

0 commit comments

Comments
 (0)