Configuration Manager Automation: A Guide to Exporting and Importing Queries using PowerShell

As a Configuration Manager (SCCM or MEMCM) administrator, you likely know the importance of managing and organizing queries within your environment. These queries are used to gather information about devices, software, and other components within your environment, making them essential for tracking and managing your infrastructure. However, managing these queries can be time-consuming and repetitive, especially when working with many queries. This is where automation comes in.

In this blog post, we’ll look at how you can automate exporting and importing SCCM queries using PowerShell scripts. The two scripts provided below will make it easy for you to backup and restore your Configuration Manager queries, which can be useful for migrating queries between SCCM environments or for keeping a backup of the queries for future reference.

Export Queries using PowerShell

				
					<#
    =================================================================================
    DISCLAIMER:
    This script is provided "as-is" with no warranties. Usage of this script is at
    your own risk. The author is not liable for any damages or losses arising from
    using this script. Please review the full legal disclaimer at:
    https://kaidojarvemets.com/legal-disclaimer/
    =================================================================================
#>
#Get all queries from the Cofiguration Manager
$Queries = Get-CMQuery
foreach($Query in $Queries){

    Write-Output -InputObject "Exporting $($Query.Name)"
    #Export all the queries
    Export-CMQuery -Name $Query.Name -ExportFilePath "C:\TEMP\$($Query.Name).mof"

}
				
			

Import Queries using PowerShell

				
					<#
    =================================================================================
    DISCLAIMER:
    This script is provided "as-is" with no warranties. Usage of this script is at
    your own risk. The author is not liable for any damages or losses arising from
    using this script. Please review the full legal disclaimer at:
    https://kaidojarvemets.com/legal-disclaimer/
    =================================================================================
#>
#Get all queries from the C:\TEMP folder
$SavedQueries = Get-ChildItem -Path "C:\TEMP"
foreach($SavedQuery in $SavedQueries){
    
    Write-Output -InputObject "Importing $($SavedQuery.FullName)"
    #Import all the query
    Import-CMQuery -ImportFilePath $SavedQuery.FullName
}
				
			

Leave a Comment

Contact me

If you’re interested in learning about Configuration Manager Automation: A Guide to Exporting and Importing Queries using PowerShell. I can help you understand how this solution can benefit your organization and provide a customized solution tailored to your specific needs.

Table of Contents