Renaming Multiple SCCM collections

I have a bunch of collections I want to rename really just remove a version number and keep the rest of the name the same . I have a script to rename one at at time but not sure how I can do it for multiple… was thinking of importing a csv or excel spreadsheet that had one column with oldname and one cloumn iwth new and have it loop through each . However I’m not sure what I need to do . Do I use a for each statement ?. Is there a way I can get all the collectiosn with the version and then some how remove that and set the new name… Hope this makes sense, Powershell beginner here.

Press ‘F5’ to run this script. Running this script will load the ConfigurationManager

module for Windows PowerShell and will connect to the site.
This script was auto-generated at ‘9/22/2022 4:34:53 PM’.
Site configuration
$SiteCode = “XXX” # Site code
$ProviderMachineName = “XXXXXXXXX.XX-XXXXXX.COM” # SMS Provider machine name

Customizations
$initParams = @{}
#$initParams.Add(“Verbose”, $true) # Uncomment this line to enable verbose logging
#$initParams.Add(“ErrorAction”, “Stop”) # Uncomment this line to stop the script on any errors

Do not change anything below this line
Import the ConfigurationManager.psd1 module
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module “$($ENV:SMS_ADMIN_UI_PATH)..\ConfigurationManager.psd1” @initParams
}

Connect to the site’s drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}

Set the current location to be the site code.
Set-Location "$($SiteCode):" @initParams

$CollName = “TEST 2.5”

$NewCollName = “TEST”

Get-CMCollection -Name $CollName

Set-CMCollection -Name $CollName -NewName $NewCollName -Verbose

currney,
Welcome to the forum. :wave:t4:

Before we proceed … it is hard to distinguish between plain text and code. So please go back, edit your question again and fix the formatting of the code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

I have edited and used preformatted text option looks much better thanks.

It does indeed. :+1:t4:

I don’t have access to an SCCM system at the moment to test but if you really just want to strip some collection names from unwanted parts there might be an easier way.

You need to set up an according pattern descibing the names you want to change but then it should be as easy as this:

Get-CMCollection | 
    Where-Object -Property Name -Match -Value 'Your Pattern' |
        ForEach-Object {
            $NewCollName = $_.Name -replace 'Pattern to remove'
            Set-CMCollection -Name $_.Name -NewName $NewCollName -Verbose
        }

Thanks for responding but I think im going to have to use a spread sheet with old and new names.Becuase although the version number is the same in all the collection names I want to remove it from. The rest of the collection are different with a different pattern…I hope that makes sense

Actually I see how yours might work now that I look at it again I will try it . Can u provide a option using a spreadsheet or csv file??? Using old name new name. Thanks for all of your help.

It depends pretty much on the patterns. If there is a rule you could explain to someone there’d probably a way to filter them by a sophisticated regex pattern.

You may share some (sanitized) sample names of collections you want to rename, some names you want to keep and how the new names should look like.

Why 3 question marks? :thinking:

You can import a CSV file with

then you can use Foreach-Object just like in my code suggestion.

Please always read the help for the cmdlets you’re about to use COMPLETELY including the examples to learn how to use them.

Thanks for your help it is verybmuch appreciated… So all collections names would contain the following text for eg. “test 6.15” with different text before and after . It depends on what the collection is used as to what text appears before and after “test 6 15” . I just want to remove the 6.15 As for why I put the tree question marks p above i my question about csv I have no idea … so with the csv option how would you set it up to get the name from column A then trname with column b Thanks.

If it’s really just this it’s the most easiest task ever. You query all collections having 6 15 in their names and rename them using -replace '6 15'

Read the help for Import-Csv including the examples to learn how to use it.

A proper CSV file would have headers. So you import the data from your CSV, pipe them to Foreach-Object using the header names as property names for the pipeline variable $_. So for example $_.CurrentName and $_.TargetName

Thanks for all of you help

I have a csv with two columns oldcollectionanme , newcollectionname and I have tried to import and use the foreach statement to reanme collection from Oldcollectionname to newcollection name with no luck The import works and when I Type $collections I see both columns old and new . Here is what I have

# Site configuration
$SiteCode = "XXX" # Site code 
$ProviderMachineName = "xxxxxxxxxxxxxxxxxxxxx" # SMS Provider machine name

# Customizations
$initParams = @{}
#$initParams.Add("Verbose", $true) # Uncomment this line to enable verbose logging
#$initParams.Add("ErrorAction", "Stop") # Uncomment this line to stop the script on any errors

# Do not change anything below this line

# Import the ConfigurationManager.psd1 module 
if((Get-Module ConfigurationManager) -eq $null) {
    Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams 
}

# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
    New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}
# Set the current location to be the site code
Set-Location "$($SiteCode):\" @initParams

#Define output
$Output=@()

#Import CSV with Old and New Collection Names
$Collections= import-csv -Path "C:\temp\CollectionRename.csv"

#Rename Old Collections and write to Screen and to Output file
foreach($COllectionName in $Collections){
    If ( Set-CMCollection -name $_.OldcollectionName -NEWNAME $_.Newcollectionanme){
        Output+1= "$_NewCollectionName, rename successful"
        Write-Host "NewCollectionName, Rename successful" - ForegroudCOlor Green
    }
    else {
        Output+1= "$_OldCollectionName, rename successful"
        Write-Host "$_OldCollectionName, Rename successful" - ForegroudCOlor Red
        }
}
$Output | Out-file "C:\TEMP\CollectionRenameResults.csv"

There are several issues with your code. Don’t you get error messages when you run it this way? :thinking:

I’d use something like this:

$CollectionList = Import-Csv -Path 'C:\temp\CollectionRename.csv'
$Output =
foreach ($Collection in $CollectionList ) {
    try {
        $Result = 
            Set-CMCollection -Name $Collection.OldcollectionName -NewName $Collection.NewCollectionName -PassThru -ErrorAction Stop
        [PSCustomObject]@{
            CSVOldName       = $Collection.OldcollectionName
            CSVNewName       = $Collection.NewCollectionName
            CMCollectionName = $Result.Name
            CollectionID     = $Result.CollectionID
        }
    }
    catch {
        Write-Error "Error renaming '$($Collection.OldcollectionName)' to '$($Collection.NewCollectionName)'"
    }
}
$Output | 
    Export-Csv -Path 'C:\TEMP\CollectionRenameResults.csv' -NoTypeInformation

Instead of an if statement I’d use proper error handling to catch possible errors.
With the parameter -Passthru the cmdlet returns an object that represents the item with which you’re working. You can use this for your output object.

You should always read the COMPLETE help for the cmdlets you’re about to use to learn how to use them and what they are capable of.

Thanks very much for your assistance… Yes I did get errors and worked through some of them but just got frustrated as the scrpt did not seem to be reading my varaibles for Oldcollcetionname and Newcollection name . It did output them once when I use Set-CMCollection -Name Inputobject $oldcollectionname -NewName $ NewcollectionName but then bombed.

If you have errors you cannot get rid of by yourself it is crucial that you share them here completely (formatted as code as well, please) because they are an important language feature telling you what’s wrong. And sometimes they even tell how to do it right. :wink:

When you use a foreach loop instead of a Foreach-Object loop you don’t have the pipeline variable $_. Instead you use the loop variable you define inside your loop statement foreach($LoopVariable in $ArrayVariable):wink:

I was having issues with foreach-object as well…I guess I just don’t understand well enough how this are evalualted and passed along .

Ran the code you provided and it failed on _NewName but I have no idea why don’t understand the custom object piece.

PS CAS:\> 
$CollectionList = Import-Csv -Path 'C:\temp\CollectionRename.csv'
$Output =
foreach ($Collection in $CollectionList ) {
    try {
        $Result = 
            Set-CMCollection -Name $Collection.OldcollectionName -NewName $Collection.Newcollectionanme -PassThru -ErrorAction Stop
        [PSCustomObject]@{
            CSVOldName       = $Collection.OldcollectionName
            CSVNewName       = $Collection.Newcollectionanme
            CMCollectionName = $Result.Name
           #CollectionID     = $Result.CollectionID
        }
    }
    catch {
        Write-Error "Error renaming '$($Collection.OldcollectionName)' to '$($Collection.Newcollectionanme)'"
    }
}
$Output | 
    Export-Csv -Path 'C:\TEMP\CollectionRenameResults.csv' -NoTypeInformation
$CollectionList = Import-Csv -Path 'C:\temp\CollectionRename.csv'
$Output =
foreach ($Collection in $CollectionList ) {
    try {
        $Result = 
            Set-CMCollection -Name $Collection.OldcollectionName -NewName $Collection.Newcollectionanme -PassThru -ErrorAction Stop
        [PSCustomObject]@{
            CSVOldName       = $Collection.OldcollectionName
            CSVNewName       = $Collection.Newcollectionanme
            CMCollectionName = $Result.Name
           #CollectionID     = $Result.CollectionID
        }
    }
    catch {
        Write-Error "Error renaming '$($Collection.OldcollectionName)' to '$($Collection.Newcollectionanme)'"
    }
}
$Output | 
    Export-Csv -Path 'C:\TEMP\CollectionRenameResults.csv' -NoTypeInformation : Error renaming 'Windows Server xxxxxxxxxxxx 7.15' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
 
$CollectionList = Import-Csv -Path 'C:\temp\CollectionRename.csv'
$Output =
foreach ($Collection in $CollectionList ) {
    try {
        $Result = 
            Set-CMCollection -Name $Collection.OldcollectionName -NewName $Collection.Newcollectionanme -PassThru -ErrorAction Stop
        [PSCustomObject]@{
            CSVOldName       = $Collection.OldcollectionName
            CSVNewName       = $Collection.Newcollectionanme
            CMCollectionName = $Result.Name
           #CollectionID     = $Result.CollectionID
        }
    }
    catch {
        Write-Error "Error renaming '$($Collection.OldcollectionName)' to '$($Collection.Newcollectionanme)'"
    }
}
$Output | 
    Export-Csv -Path 'C:\TEMP\CollectionRenameResults.csv' -NoTypeInformation : Error renaming '7.15' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
 
$CollectionList = Import-Csv -Path 'C:\temp\CollectionRename.csv'
$Output =
foreach ($Collection in $CollectionList ) {
    try {
        $Result = 
            Set-CMCollection -Name $Collection.OldcollectionName -NewName $Collection.Newcollectionanme -PassThru -ErrorAction Stop
        [PSCustomObject]@{
            CSVOldName       = $Collection.OldcollectionName
            CSVNewName       = $Collection.Newcollectionanme
            CMCollectionName = $Result.Name
           #CollectionID     = $Result.CollectionID
        }
    }
    catch {
        Write-Error "Error renaming '$($Collection.OldcollectionName)' to '$($Collection.Newcollectionanme)'"
    }
}
$Output | 
    Export-Csv -Path 'C:\TEMP\CollectionRenameResults.csv' -NoTypeInformation : Error renaming 'DEPLOY - xxxxxxxxxxxxxxxxxxxxxx - Wave 2' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
 
$CollectionList = Import-Csv -Path 'C:\temp\CollectionRename.csv'
$Output =
foreach ($Collection in $CollectionList ) {
    try {
        $Result = 
            Set-CMCollection -Name $Collection.OldcollectionName -NewName $Collection.Newcollectionanme -PassThru -ErrorAction Stop
        [PSCustomObject]@{
            CSVOldName       = $Collection.OldcollectionName
            CSVNewName       = $Collection.Newcollectionanme
            CMCollectionName = $Result.Name
           #CollectionID     = $Result.CollectionID
        }
    }
    catch {
        Write-Error "Error renaming '$($Collection.OldcollectionName)' to '$($Collection.Newcollectionanme)'"
    }
}
$Output | 
    Export-Csv -Path 'C:\TEMP\CollectionRenameResults.csv' -NoTypeInformation : Error renaming 'DEV - xxxxxxxxxxxxxxxxxxxxx - BBTS' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
 
$CollectionList = Import-Csv -Path 'C:\temp\CollectionRename.csv'
$Output =
foreach ($Collection in $CollectionList ) {
    try {
        $Result = 
            Set-CMCollection -Name $Collection.OldcollectionName -NewName $Collection.Newcollectionanme -PassThru -ErrorAction Stop
        [PSCustomObject]@{
            CSVOldName       = $Collection.OldcollectionName
            CSVNewName       = $Collection.Newcollectionanme
            CMCollectionName = $Result.Name
           #CollectionID     = $Result.CollectionID
        }
    }
    catch {
        Write-Error "Error renaming '$($Collection.OldcollectionName)' to '$($Collection.Newcollectionanme)'"
    }
}
$Output | 
    Export-Csv -Path 'C:\TEMP\CollectionRenameResults.csv' -NoTypeInformation : Error renaming 'PAT - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

Why do you post the code 6 times? And it’s actually the code I suggested!? And it is mixed together with the error messages. :man_shrugging:t4:

Please before you really post a question or a reply check it at least once before you hit the button.

 : Error renaming 'Windows Server xxxxxxxxxxxx 7.15' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

Do you have a collection with the name Windows Server xxxxxxxxxxxx 7.15? And what should be the new name?

: Error renaming '7.15' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

Do you have a collection with the name 7.15? And what should be the new name?

 : Error renaming 'DEPLOY - xxxxxxxxxxxxxxxxxxxxxx - Wave 2' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

Do you have a collection with the name DEPLOY - xxxxxxxxxxxxxxxxxxxxxx - Wave 2? And what should be the new name?

 : Error renaming 'DEV - xxxxxxxxxxxxxxxxxxxxx - BBTS' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

Do you have a collection with the name DEV - xxxxxxxxxxxxxxxxxxxxx - BBTS? And what should be the new name?

 : Error renaming 'PAT - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS' to ''
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

Do you have a collection with the name PAT - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxS? And what should be the new name?

… and ss on and on … :smirk:

Take look at your input CSV. There seems to be something wrong Id’ say. :point_up_2:t4:

That’s not helpful at all … what issues? Please keep in mind that we cannot see your screen and we cannot read your mind.

Read the according help topics … completely including the examples and play around with it a little bit.

Yes I have collections by those names in my CSV under column oldcollectonanme which is separated by a comma then the second column is Newcollectionname listing the new collection names . When I get the output from $collectionslist it shows what I just described Two Separate Columns with one with oldname one with new name I think what is happening is its never getting the new collection name and sorry for printng the same error 6 times and yes I realize you cannot see my screen. Here is the output from $CollectionlList

$CollectionList

OldCollectionName                                      NewCollectionName                                
-----------------                                      -----------------                                
Windows Server 2016 - Citrix 7.15                      Windows Server 2016 - CITRIX                     
7.15                                                   CITRIX 7.15                                      
DEPLOY - DEV - CITRIX 7.15 Server 2016 -PTSTS - Wave 2 DEPLOY - DEV - CITRIX Server 2016 -PTSTS - Wave 2
DEV - CITRIX 7.15 Server 2016 - BBTS                   DEV - CITRIX Server 2016 - BBTS                  
PAT - CITRIX 7.15 Server 2016 - DCJ PBTS               PAT - CITRIX Server 2016 - DCJ PBTS

Maybe i need quotes around the collectons names because of spaces?? Olaf I really appreciate your assistance and I understand you can’t see my screen and I apologize for posting the same error 6 times when only once was needed. I just grabbed the whole thing . YEs all of the collection that errored are in the csv. It looks like it never gets the NewCollectionName and thats why it fails

You are actually right. And I now know why. :man_shrugging:t4:

I made a typo and you did not see it as well. :wink: … sorry.

Instead of

$Collection.Newcollectionanme

it has to be

$Collection.NewCollectionName
1 Like