SCCM | Intune | Windows 11 Forums

Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members. Please post your questions in the correct category.

NEW Powershell to add multiple devices to a collection from txt but devices must be a 'Like' clause and not exact


New Member
thanks so much for the article to show us how to add devices to a collection through powershell, it works great.

but i have another scenario that i need help with. so i have a list of 200 user accounts the company's device naming policy is that the machine name will always be first name and first 3 letters of the second name eg: Elvis Presley machine name will be Elvispr-wrk or Elvispr-lap

So i would like to modify the script to say... look at the txt file and add any device name that is like Elvispr%.

below is an extract of the script from the article above.

Get-Content "E:\Sources\Import\List_computers.txt" | foreach { Add-CMDeviceCollectionDirectMembershipRule -CollectionName "Computer List" -ResourceID (Get-CMDevice -Name $_).ResourceID }
You can just use the Wildcard (*) within the Get-CMDevice function.
Get-Content "E:\Sources\Import\List_computers.txt" | foreach {
        Add-CMDeviceCollectionDirectMembershipRule -CollectionName "Computer List" -ResourceID (Get-CMDevice -Name "$_*").ResourceID