Create an SCCM folder with Powershell WMI

Being a IT professional leads to many functions :). Here is one for creating a container node aka. SCCM folder.

The function uses the write-log function which you can find here

In this case I have defined two global variables in top of the script so its not needed to declare server and site when calling the function:

$GlobalSCCMSiteServer = "SCCM2016"
$GlobalSCCMSiteCode = "S01"

First I fetch the parent node ID with this query, if not used, the folder will be in the first level of folders.
In this example we fetch the folder id for ‘Hardware limited collections’ in the device collection instance:

$ParentFolderName = "Hardware limited collections"
$CollectionNodeID = (Get-WmiObject -Namespace "Root\SMS\Site_$GlobalSCCMSiteCode" -Class SMS_ObjectContainerNode -ComputerName $GlobalSCCMSiteServer -Filter "Name = '$ParentFolderName'").ContainerNodeID
Function Create-SCCMFolder
{
	param
	(   [Parameter(Mandatory=$False)]
		$SCCMSiteServer =$GlobalSCCMSiteServer,
        [Parameter(Mandatory=$False)]
		$SCCMSiteCode = $GlobalSCCMSiteCode,
        [Parameter(Mandatory=$True)]
		$FolderName,
        [Parameter(Mandatory=$False)]
        [switch]$UserFolder,
        [Parameter(Mandatory=$False)]
        [switch]$DeviceFolder,
        [Parameter(Mandatory=$False)]
		$ParentCollNodeID = "0"
	)
	$FolderInstance = ([wmiclass]("\\$SCCMSiteServer\ROOT\sms\site_" + $SCCMsiteCode + ":SMS_ObjectContainerNode")).CreateInstance()
	if ($FolderInstance)
	{
		$FolderInstance.Name = $FolderName
        if($DeviceFolder){$FolderInstance.ObjectType = "5000"}
		if($UserFolder){$FolderInstance.ObjectType = "5001"}
		$FolderInstance.ParentContainerNodeID = $ParentCollNodeID
		$FolderInstance.Put() | Out-Null
		Write-Log ("Created new collection folder {0}" -f $FolderName) -Path $Log
	}
	else
	{
		Write-Log -LogOutput ("There was an error with creating the folder {0}: {1}" -f $FolderName, $_) -Path $Log
	}
}

Here I use the parent folder ID to create a subfolder in ‘Hardware limited collections’ in the device collections instance:

Create-SCCMFolder -FolderName "Lenovo T460" -ParentCollNodeID $CollectionNodeID -DeviceFolder

Here I use the function to create a main/root folder in the device collections instance:

Create-SCCMFolder -FolderName "Tablets only" -DeviceFolder

The same can be done for the user collections instance:

Create-SCCMFolder -FolderName "All consultants" -UserFolder

Screenshot of the output:

You can read more about the SMS_ObjectContainerNode class here

Please write a comment for help or suggestions

2 thoughts on “Create an SCCM folder with Powershell WMI”

Leave a comment