SCCM Configmgr 2012 Powershell script How to delete multiple packages applications Driver packages what else ?

Long Ago,wrote a powershell script to add packages,applications,driver packages,Boot Images etc to selected Distribution Points,more information http://eskonr.com/2014/02/configmgr-2012-updated-powershell-script-add-packages-applications-drivers-to-distribution-point/

As Configmgr admin,we will be doing some maintenance cleanup activities monthly or quarterly for applications,packages,drivers ,collection etc.I will write another post  on ,how to identify the packages which are not used from longer period by Configmgr clients.

Removing single package from console is straight forward .Right click on package and click delete.This process takes few steps asking to go through the prompt to answer or delete directly.

I have collected several packages with different package types like applications,driver packages,SUP packages etc using the SQL quires based on the usage and i decided to delete all of them. How do delete all ?

Search each package where do they reside in console and delete one by one manually ? If you have no better job than this,then yes,you can of course go with manual method to identify where they reside in Console and delete it .

Powershell gives so much flexible to perform most of the configmgr jobs with single command line.

I wrote a powershell script-looks like very lengthy since the commands for package,application type,SUP,image are different and i have used several if conditions to check if the package really deleted from console or not not before it write the result to output file .

This script basically take the information from the txt file , what you supplied and based on the package type (like switch case here),it will jump to the specific category and perform the deletion.

What is required before you run the script  ?

1.Enough permissions to delete the content from Configmgr console

2. Get list of all package names (script is written based on its Name instead of Package ID) either from Console or SQL database along with its package Type.

3. create excel or notepad with format shown below:

packages1

 

do some excel work to replace the space with comma(,) and save it .

output of CSV should look like this :

packages2

 

You don’t require Excel to be installed on the computer to run the script .Notepad is enough to read for Powershell.

Powershell Script :

#######################################################################
#Name: Remove multiple packages ,applications,drivers,Images etc from Configmgr 2012
#Author: Eswar Koneti
#Date Created:23-July-2014
#Avilable At:www.eskonr.com

#######################################################################

#Import Module
$CMModulepath=$Env:SMS_ADMIN_UI_PATH.ToString().Substring(0,$Env:SMS_ADMIN_UI_PATH.Length -  5)+"\ConfigurationManager.psd1"
import-module $CMModulepath -force
#Change the site Code
CD PRI:
#Change output File Location to store the results
$Outputpath='C:\Users\eswar\Desktop\Removed-packages.txt'
#Change the input filename ,make sure you supply the information in correct format

Import-Csv C:\Users\eswar\Desktop\Remove-packages.csv |`
ForEach-Object {
$PackageType = $_.PackageType
$PackageName = $_.PackageName
# For Packages

            If($PackageType -eq "Package")
{
#Check if the supplied package exist in CM12
if ( Get-CMPackage -Name "$PackageName")
{
Remove-CMPackage -Name  "$PackageName" -Force
#Check if the supplied package deleted or not from CM12
if (!( Get-CMPackage -Name "$PackageName"))
{
"Package " +  $PackageName + " "+ "Deleted " | Out-File -FilePath $Outputpath -Append
}
else
{
"Package " +  $PackageName + " "+ "Not Deleted ,Fix the problem and delete manually " | Out-File -FilePath $Outputpath -Append
}
}
}

#For Applications

            If($PackageType -eq "Application")
{
#Check if the supplied Application exist in CM12
if ( Get-CMApplication -Name "$PackageName")
{
Remove-CMApplication -Name  "$PackageName" -Force
#Check if the supplied Application deleted or not from CM12
if (!( Get-CMApplication -Name "$PackageName"))
{
"Application " +  $PackageName + " "+ "Deleted " | Out-File -FilePath $Outputpath -Append
}
else
{
"Application " +  $PackageName + " "+ "not Deleted ,Fix the problem and delete Manually " | Out-File -FilePath $Outputpath -Append
}
}

                }

#For Driver Packages
If($PackageType -eq "Driver")
{
#Check if the supplied Driver Package exist in CM12
if ( Get-CMDriverPackage -Name "$PackageName")
{
Remove-CMDriverPackage -Name  "$PackageName" -Force
#Check if the supplied Driver Package deleted or not from CM12
if (!( Get-CMDriverPackage -Name "$PackageName"))
{
"Driver " +  $PackageName + " "+ "Deleted " | Out-File -FilePath $Outputpath -Append
}
else
{
"Driver " +  $PackageName + " "+ "not Deleted ,Fix the problem and delete Manually " | Out-File -FilePath $Outputpath -Append
}

                    }
}

#For BootImages

            If($PackageType -eq "BootImage")
{
#Check if the supplied Boot Image exist in CM12
if ( Get-CMBootImage -Name "$PackageName")
{
Remove-CMBootImage -Name  "$packagename" -Force
#Check if the supplied Boot Image deleted or not from CM12
if (!( Get-CMDriverPackage -Name "$PackageName"))
{
"BootImage " +  $PackageName + " "+ "Deleted " | Out-File -FilePath $Outputpath -Append
}
else
{
"BootImage " +  $PackageName + " "+ "not Deleted ,Fix the problem and delete Manually " | Out-File -FilePath $Outputpath -Append
}
}
}

#For OSImage
If($PackageType -eq "OSImage")
{
#Check if the supplied OS Image exist in CM12
if ( Get-CMOperatingSystemImage -Name "$PackageName")
{
Remove-CMOperatingSystemImage -Name "$packagename" -Force
#Check if the supplied OS Image deleted or not from CM12
if (!( Get-CMOperatingSystemImage -Name "$PackageName"))
{
"OSImage " +  $PackageName + " "+ "Deleted " | Out-File -FilePath $Outputpath -Append
}
else
{
"OSImage " +  $PackageName + " "+ "not Deleted ,Fix the problem and delete Manually " | Out-File -FilePath $Outputpath -Append
}

                   }
}

#For SUPPackages
If($PackageType -eq "SUP")
{
#Check if the supplied SUP Package exist in CM12
if ( Get-CMSoftwareUpdateDeploymentPackage -Name "$PackageName")
{
Remove-CMSoftwareUpdateDeploymentPackage -Name  "$packagename" -Force
#Check if the supplied SUP Package exist in CM12
if (!( Get-CMSoftwareUpdateDeploymentPackage -Name "$PackageName"))
{
"SUP " +  $PackageName + " "+ "Deleted " | Out-File -FilePath $Outputpath -Append
}
else
{
"SUP  " +  $PackageName + " "+ "not Deleted ,Fix the problem and delete Manually " | Out-File -FilePath $Outputpath -Append
}
}
}

}

 

 

To know more about Configmgr Powershell cmd-lets,refer http://technet.microsoft.com/en-us/library/jj821831(v=sc.20).aspx

If you have any difficulties with above script,download it via TechNet Gallery: http://gallery.technet.microsoft.com/SCCM-Configmgr-2012-dfbe62e6

10 Responses to "SCCM Configmgr 2012 Powershell script How to delete multiple packages applications Driver packages what else ?"

  1. Thanks for providing this script, simple but really helpful. It seems the script stops continuing after CD command, is there any continue statement to be given for this to be followed?

    Reply
      1. Hi Eshwar,

        Thank you for the response. Just would like to see if you have any suggestion for me. I am intending to delete the packages along with source files.

        Thanks,
        Seemon

        Reply
        1. Hi Seemonraj,
          You can extend the script to get the Content location path for the particular package before deleting the package from console and after the package is deleted from console ,delete the source files also.
          The script should do something like below:
          Get the package location source files for the particular package and store it in variable
          Delete the package from console
          delete the source files that stored in first step.

          Thanks,
          Eswar

          Reply

Leave a Reply to PhiBu Cancel reply