PowerShell - Uninstall SCCM Clients

Sokoban 1,036 Reputation points
2023-11-08T10:50:25.83+00:00

Hi I have a PowerShell Scripts to remove Configuration Clients:

$VerbosePreference =   "Continue"
$Computers         =   Get-Content "C:\Scripts\All_MDM_Clients.txt"
foreach ($ComputerName in $Computers){
        Invoke-Command -ComputerName ($ComputerName) -ScriptBlock {
            if (Test-Path 'C:\windows\CCM'){
                Write-Output  "$env:ComputerName  : Script is Running"
                get-service ccmexec  -ea SilentlyContinue | 
                Stop-Service -Force -Verbose
                Remove-Item -Path "$($Env:WinDir)\CCM" -Force  -Confirm:$false -Verbose 
                Remove-Item -Path "$($Env:WinDir)\CCMSetup" -Force -Confirm:$false  -Verbose 
                Remove-Item -Path "$($Env:WinDir)\CCMCache" -Force -Confirm:$false  -Verbose 
                Remove-Item -Path "$($Env:WinDir)\smscfg.ini" -Force -Confirm:$false  -Verbose 
                Remove-Item -Path 'HKLM:\Software\Microsoft\SystemCertificates\SMS\Certificates\*' -Force -Confirm:$false  -Verbose 
                Remove-Item -Path 'HKLM:\SOFTWARE\Microsoft\CCM' -Force -Recurse -Verbose
                Remove-Item -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\CCM' -Force -Recurse -Confirm:$false  -Verbose
                Remove-Item -Path 'HKLM:\SOFTWARE\Microsoft\SMS' -Force -Recurse -Confirm:$false  -Verbose
                Remove-Item -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\SMS' -Force -Recurse -Confirm:$false   -Verbose
                Remove-Item -Path 'HKLM:\Software\Microsoft\CCMSetup' -Force -Recurse -Confirm:$false  -Verbose
                Remove-Item -Path 'HKLM:\Software\Wow6432Node\Microsoft\CCMSetup' -Force -Confirm:$false  -Recurse  -Verbose
                Remove-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\CcmExec' -Force -Recurse -Confirm:$false  -Verbose
                Get-CimInstance -query "Select * From __Namespace Where Name='CCM'" -Namespace "root" | 
                Remove-CimInstance -Verbose -Confirm:$false 
                Get-CimInstance -query "Select * From __Namespace Where Name='CCMVDI'" -Namespace "root" | 
                Remove-CimInstance -Verbose -Confirm:$false 
                Get-CimInstance -query "Select * From __Namespace Where Name='SmsDm'" -Namespace "root" | 
                Remove-CimInstance  -Verbose -Confirm:$false 
                Get-CimInstance -query "Select * From __Namespace Where Name='sms'" -Namespace "root\cimv2" | 
                Remove-CimInstance -Verbose -Confirm:$false 
                Write-Output  "$env:ComputerName  : Script Ended"
            }
            else {
                Write-Warning " $env:ComputerName : Setup files not found, Verify the client installation"
            }
        }
}

The error messages is :

Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri
parameter, or pass URI objects instead of strings.
At line:2 char:9
+         Invoke-Command -ComputerName $ComputerName -ScriptBlock {
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
    + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand

All_MDM_Clients.txt looks like that:

User's image

Please help :-)

// Sokoban

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,526 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 33,736 Reputation points
    2023-11-09T02:18:06.1166667+00:00

    You probably have blank lines in the file.

    $VerbosePreference =   "Continue"
    $Computers         =   Get-Content "C:\Scripts\All_MDM_Clients.txt"
    foreach ($ComputerName in $Computers){    
        if ($ComputerName.trim() -ne "") {
            Write-Output "Processing $ComputerName"  
            Invoke-Command -ComputerName ($ComputerName) -ScriptBlock {
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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