PowerShell where-object not equal [With examples]

PowerShell Where-Object allows you to filter objects based on their properties and values. We will see how to use the -ne operator with PowerShell Where-Object. We will see a complete example of “PowerShell where-object not equal“.

In PowerShell, the Where-Object cmdlet is used to filter objects based on their properties, and the -ne operator stands for “not equal”. To filter objects where a specific property does not equal a certain value, you can use a command like Get-Process | Where-Object { $_.ProcessName -ne ‘svchost’ }, which will return all processes whose name is not ‘svchost’. This combination allows for precise filtering in scripts and command-line operations.

PowerShell where-object not equal

The Where-Object cmdlet in PowerShell allows you to select objects that have particular property values from a collection of objects. For example, if you have a list of files and you want to filter out files that are not of a certain type, Where-Object can be used to do just that.

The Not Equal (-ne) Operator

The not equal operator -ne in PowerShell is used to compare two values or expressions and determine if they are not the same. When used with Where-Object, it filters out the objects that do not match the specified condition.

Examples of PowerShell Where-Object with -ne

Let’s go through some practical examples to understand how Where-Object and -ne work together in PowerShell. Let us check some PowerShell where-object not equal examples.

Example 1: Filtering Process Objects

Imagine you want to get a list of all processes running on your system that are not named “svchost”. You can use the PowerShell Where-Object not equal operator.

Get-Process | Where-Object { $_.ProcessName -ne 'svchost' }

This command gets all the processes running on your system and uses Where-Object to filter out the processes where the ProcessName is not equal to “svchost”.

You can see the output in the screenshot below after I executed the PowerShell script using VS code.

powershell where-object not equal

Example 2: Excluding Specific Files from a List

If you have a list of files and want to exclude files with a certain extension, for instance, .log files, you could use the following command PowerShell Where-Object with -ne operator:

Get-ChildItem | Where-Object { $_.Extension -ne '.log' }

This command lists all items in the current directory but excludes files that end with the .log extension.

Example 3: Filtering Custom Objects

Suppose you have a custom object with a property “Status” and you want to filter out all objects that don’t have a status of “Active”:


# Define a collection of custom objects with a 'Status' property
$customObjects = @(
    [PSCustomObject]@{Name='Object1'; Status='Inactive'},
    [PSCustomObject]@{Name='Object2'; Status='Active'},
    [PSCustomObject]@{Name='Object3'; Status='Pending'},
    [PSCustomObject]@{Name='Object4'; Status='Active'},
    [PSCustomObject]@{Name='Object5'; Status='Inactive'}
)

# Filter the objects where Status is not 'Active'
$filteredObjects = $customObjects | Where-Object { $_.Status -ne 'Active' }

# Output the filtered objects
$filteredObjects

Here, $customObjects is a variable that holds a collection of objects. The Where-Object cmdlet filters out any object whose Status property is not “Active”.

Here is the output in the screenshot below:

PowerShell Where-Object with -ne

Advanced Usage with -ne

The -ne operator can also be used with more complex expressions and multiple conditions. For instance, if you want to filter services that are not running and not set to automatic start:

Get-Service | Where-Object { $_.Status -ne 'Running' -and $_.StartType -ne 'Automatic' }

This command gets all services and filters out those that are not running and not set to start automatically.

Conclusion

The Where-Object cmdlet combined with the -ne operator is very useful in PowerShell for filtering out objects that do not match a given condition.

In this PowerShell tutorial, I have explained how to use the -ne operator with PowerShell where-object with examples.

You may also like: