Greetings everyone! This blog post is going to continue the series of new PowerShell cmdlets within System Center 2012 R2 Configuration Manager. In my last blog post, I covered how we can use Powershell to find new cmdlets from version to version of Configuration Manager, and this post will make use of that data.
This post comes from a question I received this week. We’re going to focus on interacting with the Configuration Manager folder structure using Powershell. What’s that? Folders? Configuration Manager?
That’s right. Starting with Configuration Manager 2012, folders are used for organization within the console, creating logical placeholders for all sorts of content. Most object nodes within the console will allow you to create folders. One of the benefits to folders is they are extremely useful when selecting a large number of objects, during RBA operations for instance:
Alright, now that we all know folders are a great way to keep our consoles organized, how can we mirror the folder management functionality in the console with PowerShell? We’ll begin our investigation with Powershell provider for Configuration Manager. When you open a Powershell session using the Configuration Manager module, we load a provider that allows us to interact with Configuration Manager via Powershell. We can take a look at the providers loaded by using Get-PSProvider:
Notice my prompt, PS P20:\>. This is telling me any command I issue will be handled by the Configuration Manager provider, just like any other provider. Also notice the other providers such as FileSystem, Registry, etc. This will be important.
Move-CMObject is a new cmdlet, but part of new PowerShell functionality in Configuration Manager 2012 R2. While Move-CMObject allows me to move objects within the folder hierarchy, new functionality allows me to interact with my Configuration Manager site just like the good ol' file system!
Let’s start by taking a look at our CMSite:
Awesome! So we can use Get-Childitem to take a deeper look at our site. A little known fact, in PowerShell, “Dir” is actually just an alias for “Get-ChildItem”:
Next, let’s figure out how to navigate within this hierarchy. In Powershell, we can use the Set-Location cmdlet to navigate within any of the locations from each provider. For instance, on my file system, I may want to navigate to my home directory using Set-Location, which would look like this:
This same functionality will also work in Configuration Manager:
You might say, “Hey Heath, that looks a lot like the Change Directory (cd) command from older command shells!” To that, I’d say you’re correct, we can navigate the Configuration Manager objects just like a file system!
Awesome. So, we’ve figured out how to navigate the folder structure of our site. We can also create a new folder, using the “New-Item” cmdlet. Let’s try it:
New-Item “Heath’s New Folder”Get-ChildItem | fl –Property Name
We can also make folders using an alias more of you might be familiar with. “MD”, is an alias for MKDir, which will create a new item in this path.
md “Heath’s Second Folder”get-childitem | fl –property Name
Finally, we can use the new “Move-CMObject” cmdlet in System Center 2012 R2 Configuration Manager to move objects around the console. Let’s start by getting help:
Alright, Move-CMObject will use two parameters, InputObject defines the object we are going to move, and FolderPath defines the location we’ll move to. Easy enough, let’s create a new device collection and then move it to the OSD folder:
$NewCollection = New-CMDeviceCollection -Name "Windows Server 2012 R2" -LimitingCollectionName "All Systems"Move-CMObject -FolderPath .\DeviceCollection\OSD -InputObject $NewCollection
Which has moved the newly created folder to the path we specified:
Hopefully this post has helped you understand the new ability to interact with folders in System Center 2012 R2 Configuration Manager. I’m excited for the ability to programmatically organize objects in the Configuration Manager console, as this could be an automated task to move certain objects to a specified path automatically, keeping our console nice and tidy. Feedback is always appreciated, let me know if you have any questions or comments below!