I'm working with PowerShell and have a command that retrieves information about computers using Get-ADComputer. However, I need help filtering the results to only include computers with a specific Operating System, or to exclude certain Operating Systems while still returning all other properties. The current command I'm using is:
Get-ADComputer -Filter * -Properties OperatingSystem, Created,IPv4Address, Description | Select-Object Name, OperatingSystem, Created, Description | Export-CSV -Path "C:UsersMNyUsrProfileNameDesktopAllComputers.csv" -NoTypeInformation
I'd like to modify this so I can filter based on the Operating System. Also, I've noticed that 'ADComputer' and 'Get-ADComputer' seem to return the same results. Is one just an alias for the other, or should I prefer one over the other? Thanks for the help!
5 Answers
For filtering out a specific Operating System, try using this command:
Get-ADComputer -Filter * -Properties OperatingSystem, Created,IPv4Address, Description | Where-Object {$_.OperatingSystem -ne 'Windows Server 2016 Standard'} | Select-Object Name, OperatingSystem, Created, Description | Export-CSV -Path "C:UsersMNyUsrProfileNameDesktopAllComputers.csv" -NoTypeInformation. This will exclude any computer with that OS while still giving you all the other details.
And just to clarify your question, PowerShell creates aliases for Get-* commands. For instance, using 'ADUser -Filter *' works the same as 'Get-ADUser -Filter *'. So it's really just about personal preference, but 'Get-*' is more explicit.
You might want to check out the LDAP filter option as well. It can be more powerful and easier to handle in certain cases. It's worth exploring different filter syntheses to see what works best for your needs.
To filter based on the Operating System, you can use the command with a specific filter like this:
Get-ADComputer -Properties OperatingSystem -Filter {OperatingSystem -notlike "*2016*" }
This way, you can exclude computers running Windows Server 2016. Using -Filter is generally faster than using Where-Object.
If you’re looking to filter based on a specific Operating System, try adjusting your command to something like this:
Get-ADComputer -Filter * -Properties OperatingSystem, Created,IPv4Address, Description | Where-Object {$_.OperatingSystem -ne 'Windows Server 2016 Standard'} | Select-Object Name, OperatingSystem, Created, Description | Export-CSV -Path "C:UsersMNyUsrProfileNameDesktopAllComputers.csv" -NoTypeInformation.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically