PowerShell Add Computers To Collection From CSV - SCCM ConfigMgr

Skip to primary content

Post navigation

Previous Next

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 😉

Import-AddToCollectionfromCSV.Ps1 PowerShell ################################################################################################################# #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 ###########################################################################
12345678910111213141516171819202122232425262728293031323334353637 ##################################################################################################################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 executionparam([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 actionsForeach($Computerin$Computers){#Get Device Resource ID$ResourceID=(Get-CMDevice-name$($Computer.Name)).ResourceID#Add the Device to the Collection specifiedWrite-Host"Adding Machine $($Computer.Name) ResourceID: $ResourceID"-ForegroundColorYellowadd-cmdevicecollectiondirectmembershiprule-CollectionId$CollectionID-resourceid$ResourceID-Verbose }#Set location of script back to script root.Set-Location$PSScriptRoot ###########################################################################

5 thoughts on “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

    Reply
  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

    Reply
    • You need to inport the ConfigMgr PowerShell module first. Cheers, SCCMOG

      Reply
  3. Awesome! Thanks

    The CSV file “YourCSV.csv” will be like below ###### Name server1 server2 server3 ######

    Reply
    • Happy Days! Cheers, SCCMOG

      Reply

Leave a Reply Cancel reply

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

Comment *

Name *

Email *

Website

Save my name, email, and website in this browser for the next time I comment.

Δ

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Tag » Add Computer To Sccm Powershell