PowerShell add Computers to Collection from CSV – SCCM ConfigMgr

This is a quick and dirty PowerShell script to import from CSV using the name of the machine to find the resource ID. It will only work for machines that are already a member of the Site you are working on.

For example you could use one of my other scripts to export from one collection and then add to a new collection.

As always this is provided as is, usage is in the header and please use the modded by field 😉

#################################################################################################################
#Author:   Richie Schuster - SCCMOG.COM                                                                         #
#ModdedBy: You Name Here                                                                                        #
#Script:   Import-AddToCollectionfromCSV.ps1                                                                    #
#Date:     26/06/2017                                                                                           #
#Usage:    Import-AddToCollectionfromCSV.ps1 -CollectionID S0G001BA -SiteCode S0G -CSVin "C:\Temp\YourCSV.csv"  #
#################################################################################################################

#Params for Script execution
param 
(
[parameter(mandatory=$true,HelpMessage="Please, provide a Collection ID to add the machines to e.g. P01000F4 ")][ValidateNotNullOrEmpty()][String]$CollectionID,
[parameter(mandatory=$true,HelpMessage="Please, provide a SCCM SiteCode. e.g S0G")][ValidateNotNullOrEmpty()][String]$SiteCode,
[parameter(mandatory=$true,HelpMessage="Please, provide a location to import the CSV file from with the filename. e.g C:\Temp\YourCSV.csv")][ValidateNotNullOrEmpty()][String]$CSVin
)
#Import the ConfigurationManager.psd1 module 
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"
#Set the current location to be the site code.
Set-Location "$SiteCode`:"

#$ErrorActionPreference= 'silentlycontinue'
#Get the content of the CSV file
$Computers = Import-Csv $CSVin

#Loop through each Device and perform actions
Foreach ($Computer in $Computers) {
    #Get Device Resource ID
    $ResourceID = (Get-CMDevice -name $($Computer.Name)).ResourceID
    #Add the Device to the Collection specified
    Write-Host "Adding Machine $($Computer.Name) ResourceID: $ResourceID" -ForegroundColor Yellow
    add-cmdevicecollectiondirectmembershiprule -CollectionId $CollectionID -resourceid $ResourceID -Verbose 

} 
#Set location of script back to script root.
Set-Location $PSScriptRoot

###########################################################################

5 Replies to “PowerShell add Computers to Collection from CSV – SCCM ConfigMgr”

  1. Create a file “sccc.txt” contain the list of pcs/devices name
    Click on properties of device collection and copy the collection Id
    Open power shell from SCCM
    Run the command “Get-Content “C:\Users\altaf\Desktop\sccm.txt” | foreach { Add-CMDeviceCollectionDirectMembershipRule -CollectionId PKISB0015B -ResourceID (Get-CMDevice -Name $_).ResourceID }”
    Now check you collection
    for details check

    https://gallery.technet.microsoft.com/Multiple-Computers-to-a-fdc35ffa

  2. Get-CMDevice : The term ‘Get-CMDevice’ is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:142
    + … llectionName “Malwarebyte_Group_12” -ResourceID (Get-CMDevice -Name $ …
    + ~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Get-CMDevice:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright 2016 SCCMOG | All Rights Reserved

css.php