Answered by:
Filter out Windows Server and Laptops

Question
-
Hi,
I am trying to write a script to shut down all Desktops.
I want to filter out Servers and Laptops to a variable which will not be shut down.
I can achieve filtering out all machines with Windows client OS using the below:
$computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Filter 'operatingsystem -notlike "*Windows Server*" -and enabled -eq "true"'But how can I filter out all laptops. Probably using the below:
$typ=Get-WmiObject win32_computersystem | PCSystemTypeBut, how would i be able get the whole $computer and $Type to pipe or concatenate or join to a single variable with only desktops?
Appreciate any assistance.
Wednesday, October 16, 2019 11:39 AM
Answers
-
Hi Liby,
You can use below code:
Get-ADComputer -SearchBase "DC=domain,DC=local" -Filter 'operatingsystem -notlike "*Windows Server*" -and enabled -eq "true"' | Where-Object { $(Get-WmiObject -ComputerName $($_.Name) -Class win32_computersystem | Select-Object PCSystemType -ExpandProperty PCSystemType) -eq 1 } | Select-Object Name, OperatingSystem, DistinguishedName
For Reference:
<# PCSystemType number and Values "1" = "Desktop" "2" = "Mobile / Laptop" "3"= "Workstation" "4"= "Enterprise Server" "5"= "Small Office and Home Office (SOHO) Server" "6"= "Appliance PC" "7"= "Performance Server" "8"= "Maximum" #>
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Friday, October 18, 2019 6:36 AM
- Marked as answer by Liby Sunday, November 10, 2019 5:42 AM
Thursday, October 17, 2019 4:08 PM
All replies
-
You have to do it in a loop that checks each system or use Where-Object to check the systems
\_(ツ)_/
Wednesday, October 16, 2019 12:14 PM -
Hi jrv,
Thank you. The loop. that is what is am looking for. but i cannot get the loop to work. i am a novice. If any experts can take out 5min to write the script to filter out those computers, it would be very helpful.
Thursday, October 17, 2019 4:32 AM -
Hi Liby,
You can use below code:
Get-ADComputer -SearchBase "DC=domain,DC=local" -Filter 'operatingsystem -notlike "*Windows Server*" -and enabled -eq "true"' | Where-Object { $(Get-WmiObject -ComputerName $($_.Name) -Class win32_computersystem | Select-Object PCSystemType -ExpandProperty PCSystemType) -eq 1 } | Select-Object Name, OperatingSystem, DistinguishedName
For Reference:
<# PCSystemType number and Values "1" = "Desktop" "2" = "Mobile / Laptop" "3"= "Workstation" "4"= "Enterprise Server" "5"= "Small Office and Home Office (SOHO) Server" "6"= "Appliance PC" "7"= "Performance Server" "8"= "Maximum" #>
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Friday, October 18, 2019 6:36 AM
- Marked as answer by Liby Sunday, November 10, 2019 5:42 AM
Thursday, October 17, 2019 4:08 PM -
Hi Imran,
If you want to select OperatingSystem property, you need to add "-properties OperatingSystem" with get-adcomputer cmdlet.
Get-ADComputer -SearchBase "DC=domain,DC=local" -Filter 'operatingsystem -notlike "*Windows Server*" -and enabled -eq "true"' -properties operatingsystem| Where-Object { $(Get-WmiObject -ComputerName $($_.Name) -Class win32_computersystem | Select-Object -ExpandProperty PCSystemType) -eq 1 } | Select-Object Name, OperatingSystem, DistinguishedName
Best regards,
Lee
Just do it.
Friday, October 18, 2019 6:40 AM -
Consider how this is done in PowerShell.
Get-ADComputer -SearchBase 'DC=domain,DC=local' -Filter 'Enabled -And operatingsystem -notlike "*Server*"' -properties operatingsystem | Where-Object{(Get-WmiObject win32_computersystem -ComputerName $_.Name).PCSystemType -ne 2 } | Select-Object Name, OperatingSystem, DistinguishedName
2 is for laptops. The request is "ne" to filter out.
We do not match Booleans to strings as it won't work in many cases.
There is no need to use subexpression evaluation.
Correct formatting of code is very important.
There is no need to expand anything when we only have one object.
\_(ツ)_/
- Edited by jrv Friday, October 18, 2019 7:14 AM
Friday, October 18, 2019 7:11 AM -
Thanks to Imran Khan for the script and thanks to Lee on further improving the script. But fetching the result is slow like it took 1 min to fetch 6 host that are in the same OU.
However, jvr's filter doesn't seems to work.I would like to display more fields like:
Model from Win32_computersystem class
Name (Processor) from Win32_Processor etc. for each hosts.Help is much appreciated.
Friday, October 18, 2019 10:57 AM -
Hi Liby,
This is how you can add or remove calculated properties:
Get-ADComputer -SearchBase "DC=domain,DC=local" -Filter 'operatingsystem -notlike "*Windows Server*" -and enabled -eq "true"' -properties operatingsystem| Where-Object{(Get-WmiObject win32_computersystem -ComputerName $_.Name).PCSystemType -ne 2 } | Select-Object Name, OperatingSystem, DistinguishedName, @{N="Model";E= {(Get-WmiObject -ComputerName $_.Name -Class Win32_ComputerSystem).Model}}, @{N="Processor Name";E= {(Get-WmiObject -ComputerName $_.Name -Class win32_Processor).Name}}
You can modify the condition in where-Object according to your scenario.
Friday, October 18, 2019 11:36 AM -
Thanks to Imran Khan for the script and thanks to Lee on further improving the script. But fetching the result is slow like it took 1 min to fetch 6 host that are in the same OU.
However, jvr's filter doesn't seems to work.I would like to display more fields like:
Model from Win32_computersystem class
Name (Processor) from Win32_Processor etc. for each hosts.Help is much appreciated.
You are asking incrementally for someone to write a script. Your original question has been answered. P_lease take time to learn how to write a basic script and then post specific questions about a script that you have written.
\_(ツ)_/
Friday, October 18, 2019 5:15 PM -
However, jvr's filter doesn't seems to work.The code I posted works as expected assuming the laptops do return "2" and not some other number'. If you have non-standard laptops you will need to change this to the correct number.
\_(ツ)_/
Friday, October 18, 2019 5:17 PM -
Thank you imranFriday, October 18, 2019 5:26 PM
-
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
LeePlease remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Friday, November 8, 2019 1:43 PM