Powershell SCCM Script Help

I am new to powershell scripting, I have done the basics to create scripts but I am now trying to create a script to use with SCCM and I can see it in my head but I have no clue how to implement this, find the commands and understand Object Arrays etc. So this is what I am doing. I have computer collection departments in SCCM and I want to read the list of computers and determine if a person has two computers. I will know this by the computer name. Example: AAA-JCOO-DE5040. That tells me the department, name and model computer. So I want to collect all the machines into a variable and then compare each line to one another to determine if the middle section JCOO is the same to another machine in that collection lets say AAA-JCOO-DE3050. I would then compare those two machines and Delete the 5040 from SCCM since the 3050 is the newer machine and it never got deleted out of SCCM when we did the computer upgrade. We do about 100 upgrades a month so as you can tell my Console is over run with old machines. I have over 2000 computers and do not want to do this manually. I know how to pull the computer names per collection into a variable but I am having trouble comparing the list especially the JCOO of the machine name. This is how I am getting the machines into a variable

$Admissions = Get-CMCollectionMember -CollectionName “Admissions (ADM)” | Select Name

 

Jonathan, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code or error messages or sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

If your computer names have the structure you showed above it’s very easy to split them into their parts like this:

‘AAA-JCOO-DE5040’ -split ‘-’

Now you can use this to create calculated properties you can use in a further step to group the results by the part of the name you’re after

$Admissions = Get-CMCollectionMember -CollectionName "Admissions (ADM)" | 
    Select-Object -Property Name,
                            @{Name = 'Part1'; Expression = { ($_.Name -split '-')[0] } },
                            @{Name = 'Part2'; Expression = { ($_.Name -split '-')[1] } },
                            @{Name = 'Part3'; Expression = { ($_.Name -split '-')[2] } }

Now you can group this by the seconds part of the name and extract only the computers with more than one occurrence of this part like this:

$Admissions | 
    Group-Object -Property Part2 | 
        Where-Object -Property Count -GT -Value 1 |
            Select-Object -ExpandProperty Group

BTW: You know you can set up SCCM to purge all assets haven’t had a heartbeat for an adjustable amount of days, don’t you? :wink:

Another fun method to split into vars. Fun with Powershell:

$part1,$part2,$part3 = ('AAA-JCOO-DE5040' -split '-')

Since we’re having fun :slight_smile:

$part1,$part2,$part3 = ‘AAA-JCOO-DE5040’.split(‘-’)