In this PowerShell tutorial, we will explore how to use Where-Object to filter objects that either start with a specific string or do not start with a certain string.
To filter objects in PowerShell that start with a specific string, you can use the Where-Object cmdlet with the -like operator and a wildcard character, for example, Get-ChildItem | Where-Object { $_.Name -like “log” } to find items starting with “log”. Conversely, to filter out objects that do not start with a certain string, use -notlike, such as Get-ChildItem | Where-Object { $_.Name -notlike “log” } to exclude items beginning with “log”.
PowerShell where-object starts with
To filter objects that have properties starting with a certain string in PowerShell, you can use the -like operator with the wildcard character * in Where-Object. The -like operator is used for pattern matching.
Here’s an example that filters a list of files in a directory to only include those that start with “log” with PowerShell Where-Object:
Get-ChildItem | Where-Object { $_.Name -like "log*" }In this example, Get-ChildItem retrieves all items in the current directory, and Where-Object filters to include only items where the Name property starts with “log”.
PowerShell where-object does not start with
Conversely, if you want to filter objects that do not start with a specific string in PowerShell, you can still use the -like operator but negate the condition with -not or !.
Here’s how to get all files that do not start with “log” using PowerShell Where-Object:
Get-ChildItem | Where-Object { $_.Name -notlike "log*" }This command will return all items except those whose Name starts with “log”.
Examples of PowerShell where-object starts with and does not start with
Let’s look at some practical examples of using Where-Object with the “starts with” and “does not start with” logic.
Example 1: Filtering Services
Suppose you want to check which services on your machine start with the letter “w”. You can use the following command:
Get-Service | Where-Object { $_.Name -like "w*" }This will list all services that have names beginning with “w”.
Example 2: Excluding Certain Processes
If you’re interested in viewing all processes except those that start with “svchost”, you can use:
Get-Process | Where-Object { $_.Name -notlike "svchost*" }This will provide a list of all running processes, excluding any instance of “svchost”.
Example 3: Searching for Specific File Types
Looking for all PowerShell script files in a directory that start with “config”? Use this command:
Get-ChildItem | Where-Object { $_.Name -like "config*.ps1" }This will return all .ps1 files starting with “config”.
Example 4: Excluding Files with Certain Extensions
If you want to get all files in a directory but exclude those with the .tmp extension, you could use:
Get-ChildItem | Where-Object { $_.Extension -notlike ".tmp" }Example 5: Finding Users in an Organization
If you are working with a list of users and want to find all users whose usernames start with “admin”, the command might look like this:
Get-ADUser -Filter * | Where-Object { $_.SamAccountName -like "admin*" }Note that this example assumes you have the Active Directory module loaded in PowerShell.
Example 6: Filtering Out System Accounts
To get a list of all user accounts that do not start with “NT”, which often indicates a system account, you can use:
Get-ADUser -Filter * | Where-Object { $_.SamAccountName -notlike "NT*" }Conclusion
The Where-Object cmdlet is incredibly essential for PowerShell scripting. By using the -like and -notlike operators, you can easily filter objects based on whether their properties start with or do not start with specific strings. Remember to use wildcards (*) to define the part of the string that can vary.
In this PowerShell tutorial, I have explained how to work with PowerShell where-object starts with, and PowerShell where-object does not start with.
You may also like:
- PowerShell: Where-Object vs Select-Object
- Filter Empty Values Using PowerShell Where-Object Cmdlet
- PowerShell Where-Object for Null or Empty Values
- PowerShell Where-Object in List
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.