Monday, April 2, 2018

Powershellist - Adding multiple devices to a collection with partial name search

This Powershell script will add multiple devices to a specific device collection by partial name search, based on a file with pre-specified partial and/or full device names:

Get-Content "C:\path\Collection.txt" | foreach { Add-CMDeviceCollectionDirectMembershipRule -CollectionName "Collection Name" -ResourceID (Get-CMDevice -Name *$_*).ResourceID }


It returns a "Cannot bind argument to parameter 'ResourceId' because it is null' error if the partial name is not found.  Be sure to modify the path to the file (underlined/bold) as specified (which should be a list of partial device names, one per line).  This can come in handy if, for instance, you are given a list of serial numbers for devices to add to a collection, and since in our environment the device includes the serial number in the device name, it enables us to quickly add the list of serial numbers (partial device names) to the collection.