19 thoughts on “Removing Disabled Computer Accounts from SCCM with PowerShell

  1. Thanks for the script, I ran this in my environment and it purged a few computers as expected. When I re-ran the script I get an error below…

    Exception calling “AddRange” with “1” argument(s): “Collection cannot be null.
    Parameter name: c”
    At C:\Users\administrator.VAND1\Desktop\PowerShell Scripts\CM-ComputerCleanup.ps1:134 char:28
    + $DeletedRecords.AddRange($($NotEnabledDevices| foreach {
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

    This is probably happening because there are no entries returned the second time the script is invoked. Is there a way to trap this error and have the execution of the code exit out without errors if there are no hits?

      1. Thanks for the nice script. Since I am not master in PowerShell if you could help me on the below error I will be thankful

        this error after adding below suggested line
        if ($NotEnabledDevices)
        {
        }

        Cannot convert argument “c”, with value: “@{DeviceName=(Hostanme showing ); IsDisabled=Not found in AD; Result=Successfully deleted}”, for “AddRange” to type “System.Collections.ICollection”: “Cannot convert the “@{DeviceName=Hostanme showing;
        IsDisabled=Not found in AD; Result=Successfully deleted}” value of type “System.Management.Automation.PSCustomObject” to type “System.Collections.ICollection”.”
        At line:136 char:28
        + $DeletedRecords.AddRange($($NotEnabledDevices| foreach {
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

      2. This could be because only one result is being returned therefore the AddRange() method is not recognizing it as an array, therefore not technically a range. You could try casting it into an array like this:

        $DeletedRecords.AddRange([array]$($NotEnabledDevices …

  2. Hi Trevor,
    nice script, how we can just compare sccm and ad but not delete
    just create report

    Thanks

    1. Just comment out the following line:

      Remove-CMRecord -Computername $_.ComputerName -DeviceStatus $_.IsDisabled -SiteCode $SiteCode -SiteServer $SiteServer

      1. If i just comment this line, the script return an error :

        Exception calling “AddRange” with “1” argument(s): “Collection cannot be null.
        Parameter name: c”
        At C:\Tools\CM-ComputerCleanup.ps1:133 char:28
        + $DeletedRecords.AddRange($($NotEnabledDevices| foreach {
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ArgumentNullException

  3. Hi. Very nice work. Would there be a way to specify the domain in the case of multiple trusted / Untrusted forests?

  4. thanks for this script… I get an empty return on $DeletedRecords if I do an write-host $DeletedRecords right after the following line:
    $DeletedRecords = New-Object System.Collections.ArrayList
    So it seems that New-Object instructions doesn’t work in my case. Script is ran with admin rights directly on the SCCM site server. $NotEnabledDevices does actually return the list of devices that are disabled or not found.

  5. I’m having a little bit of difficulty. I’m running the script, but not getting any output in the console nor am I getting the email. Is this confirmed working on 1710?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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