{"id":74781,"date":"2015-11-17T00:01:00","date_gmt":"2015-11-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/11\/17\/powershell-and-configuration-manager-2012-r2part-2\/"},"modified":"2019-06-18T23:26:39","modified_gmt":"2019-06-19T07:26:39","slug":"powershell-and-configuration-manager-2012-r2part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-2\/","title":{"rendered":"PowerShell and Configuration Manager 2012 R2\u2013Part 2"},"content":{"rendered":"<p><b style=\"font-size: 12px;\">Summary<\/b><span style=\"font-size: 12px;\">: Use the Configuration Manager cmdlets to work with site collections.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>\u00a0Hey, Scripting Guy!<\/p>\n<p>I deal with Configuration Manager on a regular basis, and I have to manage User and Device collections. I\u2019d like an easier way to add and remove members and audit the memberships of site collections. Could you show me how to do that with the cmdlets in Configuration Manager?<\/p>\n<p>\u2014RL<\/p>\n<p>A: Hello RL,<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here to further delve into some basic uses of those ever so magical Configuration Manager cmdlets.<\/p>\n<p><strong> \u00a0Note<\/strong>\u00a0\u00a0This is a five-part series that includes the following posts:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 1<\/a>\nLoad the Configuration Manager cmdlets and make a basic site connection.<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-2\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 2<\/a>\nUse the Configuration Manager cmdlets to work with site collections.<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-3\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 3<\/a>\nUse the Configuration Manager cmdlets to update applications in distribution points.<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-4\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 4<\/a>\nUse the Configuration Manager cmdlets to work with driver packages.<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-5\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 5<\/a>\nUse the Configuration Manager cmdlets to create an application.<\/li>\n<\/ul>\n<p>I remember having to work with collections with Configuration Manager 2007, and I wished that there was an easier way.<\/p>\n<p>To be honest, there was. It involved VBScript, WMI, and\u2026well, it was akin to chewing on foil. I didn\u2019t enjoy the process.<\/p>\n<p>Then they provided Configuration Manager cmdlets in Windows PowerShell\u2014and oh, feint! It got so much better!<\/p>\n<p>Now finding the cmdlets is actually pretty easy. The naming convention for many of the aspects of Configuration Manager reflects the cmdlet name. To identify all the cmdlets that work with collections, I can type:<\/p>\n<p style=\"margin-left: 30px;\">Get-Command \u2013module ConfigurationManager *Collection*<\/p>\n<p>I ran a quick count. 53 cmdlets are available to handle collections! That seems like a lot to work with. But if you split that between User and Device, you\u2019re about 26. Then break that down into cmdlets that <b>get<\/b> information and various other verbs, and the list is really not that scary.<\/p>\n<p>If I simply want a list of all the cmdlets that can get a collection, I can filter further:<\/p>\n<p style=\"margin-left: 30px;\">Get-Command \u2013module ConfigurationManager Get*Collection*<\/p>\n<p>This provides a much more manageable list to start with.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2364.1.PNG\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2364.1.PNG\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>We can already see cmdlets for accessing the User and Device collections. If we initially run something like <b>Get-CMDeviceCollection<\/b>, the output appears long and messy, and it is not well formatted. We can place the output in something more viewable with <b>Format-Table<\/b>:<\/p>\n<p style=\"margin-left: 30px;\">Get-CMDeviceCollection | Format-Table<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/08162.2.PNG\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/08162.2.PNG\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>It is more useful to the eyes, but it doesn\u2019t really look like the output from the console, does it? We can mitigate this with <b>Select-Object<\/b> and grab some key properties, like with any other PowerShell cmdlet:<\/p>\n<p style=\"margin-left: 30px;\">Get-CMDeviceCollection | Select-Object CollectionID, Comment, LocalMemberCount, LastRefreshTime<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/06834.3.PNG\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/06834.3.PNG\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>If you\u2019d like to identify direct memberships in a particular Device collection, you can provide the name of a Device collection and use the <b>Get-CMDeviceCollectionDirectMembershipRule<\/b> cmdlet (now that&#8217;s a mouthful!):<\/p>\n<p style=\"margin-left: 30px;\">Get-CMDeviceCollectionDirectMembershipRule \u2013CollectionName \u2018Test Deployment Group\u2019<\/p>\n<p>Or if you\u2019d like to see which Include queries exist, you can ask that question just as easily. For example, if you were curious as to the query being used to pull down the list of systems in <b>\u2018QA Systems\u2019<\/b>, you would use this line:<\/p>\n<p style=\"margin-left: 30px;\">Get-CMDeviceCollectionQuery \u2013CollectionName \u2018QA Systems\u2019<\/p>\n<p>We can even easily remove or add memberships with the cmdlets (whether they are Include-, Exclude-, Direct-, or Query-based). We can even create brand new ones on the fly with the <b>New-CMDeviceCollection<\/b> cmdlet:<\/p>\n<p style=\"margin-left: 30px;\">New-CMDeviceCollection \u2013Name \u2018HSG Collection\u2019 \u2013LimitingCollectionName \u2018All Systems\u2019<\/p>\n<p>Or as an example, I can totally remove a Device collection. Here, I remove the Device collection I just created:<\/p>\n<p style=\"margin-left: 30px;\">Remove-CMDeviceCollection \u2013Name \u2018HSG Collection\u2019<\/p>\n<p>Now if we do this, we will get a warning to confirm if we\u2019d like to perform this destructive task. If you really don\u2019t like to be asked the question, you can force it through. (Please remember before you do this, don&#8217;t practice on the production server!)<\/p>\n<p style=\"margin-left: 30px;\">Remove-CMDeviceCollection \u2013Name \u2018HSG Collection\u2019 \u2013confirm:$False -force<\/p>\n<p>Working with User collections is very similar with the exception that the cmdlet will have the word <b>User<\/b> in it, and of course, you are targeting users, not devices.<\/p>\n<p>So put your mind to work on what you need to do on a regular basis with your collections in Configuration Manager.<\/p>\n<p>RL, there is an introduction to working with collections with the Configuration Manager cmdlets. Tomorrow I&#8217;ll work with distribution points and the Configuration Manager cmdlets.<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener noreferrer\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener noreferrer\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener noreferrer\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener noreferrer\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then always remember that with great PowerShell comes great responsibility.<\/p>\n<p><b>Sean Kearney, <\/b>Honorary Scripting Guy and Cloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use the Configuration Manager cmdlets to work with site collections. \u00a0Hey, Scripting Guy! I deal with Configuration Manager on a regular basis, and I have to manage User and Device collections. I\u2019d like an easier way to add and remove members and audit the memberships of site collections. Could you show me how to [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[487,56,154,45],"class_list":["post-74781","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-configuration-manager","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use the Configuration Manager cmdlets to work with site collections. \u00a0Hey, Scripting Guy! I deal with Configuration Manager on a regular basis, and I have to manage User and Device collections. I\u2019d like an easier way to add and remove members and audit the memberships of site collections. Could you show me how to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=74781"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74781\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=74781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}