Share via

PowerShell - Compare two arrays

Matt Dillon 437 Reputation points
2022-05-27T16:29:40.293+00:00

I have to install some printers via SCCM. I am removing all previously installed printers with the Remove-Printer command and then installing the a group of printers we want installed. I will have multiple scripts based on Office locations, etc. We run a script as Admin to add the reg key to allow printers installs without a prompt first, followed by this script to install as the user, and ending with a script to remove the reg key added in the first script. It has been working fairly well as a SCCM Package, but I would like to run it as an Application and that will require a Detection Method.

I would like to use PowerShell as the Detection method. What I came up with was the following:

$Installed = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\S-1-5-21-*\Printers\Connections\*"  | Select-Object -ExpandProperty PSChildName)

This results in the network printers installed on my laptop. I need to detect that the printers I want installed are in this variable. If any additional printers are there, it does not matter - I just need to confirm that the printers I deploy are in the list.

All I can come up with is the following:

$Test = ($Installed -ccontains ",,SERVER01.domain.com,SPR-LaserJet" -and $Installed -ccontains ",,SERVER02.domain.com,CHI-CP-MAIN")
If ($Test -eq $true)
    {
    Write-Output "Success!!"
    }
else {}
    

While this will work, I was wondering if there was a better way to do this? I will have multiple scripts and some will require several printers.

Thanks in advance.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2022-05-27T20:02:43.327+00:00

Maybe something like this, instead:

$Wanted = ",,SERVER01.domain.com,SPR-LaserJet", ",,SERVER02.domain.com,CHI-CP-MAIN"

$Installed = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\S-1-5-21-*\Printers\Connections\*"  | Select-Object -ExpandProperty PSChildName)

$AllPresent = $true
$Wanted |
    ForEach-Object{
        if ($Installed -notcontains $_){
            $AllPresent = $False
        }
    }

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.