{"id":74801,"date":"2015-11-16T00:01:00","date_gmt":"2015-11-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/11\/16\/powershell-and-configuration-manager-2012-r2part-1\/"},"modified":"2019-06-18T23:27:27","modified_gmt":"2019-06-19T07:27:27","slug":"powershell-and-configuration-manager-2012-r2part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-1\/","title":{"rendered":"PowerShell and Configuration Manager 2012 R2\u2013Part 1"},"content":{"rendered":"<p><b style=\"font-size: 12px;\">Summary<\/b><span style=\"font-size: 12px;\">: Load the Configuration Manager cmdlets and make a basic site connection.<\/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\u2019ve been doing a lot of work in the Configuration Manager 2012 R2 console and it\u2019s beautiful. But I find I spend a lot of my time clicking all over the place and repeating tasks. Are there PowerShell cmdlets available that I can use?<\/p>\n<p>\u2014SH<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>\u00a0Hello SH,<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here today to give you the answer you want, which is, \u201cYes!\u201d There absolutely are cmdlets for Windows PowerShell in Configuration Manager\u00a02012\u00a0R2, and boy, are they cool! I\u2019ve had to do some repetitive tasks in Configuration Manager for a client, and I and was thinking exactly the same thing.<\/p>\n<p><strong>\u00a0 \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>My first challenge was that I couldn\u2019t actually see a shortcut to the PowerShell cmdlets. But with a quick search online, I found that they are most certainly there.<\/p>\n<p>To load the module, you need to run this code in Windows PowerShell:<\/p>\n<p style=\"margin-left: 30px;\">Import-Module \u2018C:\\Program Files (x86)\\Microsoft Configuration Manager\\AdminConsole\\bin\\ConfigurationManager.psd1\u2019<\/p>\n<p>You can now access the list of the available cmdlets by using the <b>Get-Command<\/b> cmdlet and targeting the new module:<\/p>\n<p style=\"margin-left: 30px;\">Get-Command \u2013module ConfigurationManager<\/p>\n<p>Before you get too far, remember you\u2019re going to need to bring in Help for this new module. Remember the Help is no longer built-in by default. I had initially forgotten this, and I was uttering \u201cmagic phrases\u201d when I could not find any examples to work with.<\/p>\n<p>\u2026.and then a few moments later, I smacked the front of my head realizing what I needed to do:<\/p>\n<p style=\"margin-left: 30px;\">Update-Help \u2013module ConfigurationManager<\/p>\n<p>Moments later, Help was on its way!<\/p>\n<p>Your next challenge seems trivial, but you might try to run something simple like getting a list of boundaries with the <b>Get-CMBoundary<\/b> cmdlet. You&#8217;ll run in to this little error message:<\/p>\n<p style=\"margin-left: 30px;\">Get-CMBoundary: This command cannot be run from the current drive. To run this command, you must first connect to a Configuration Manager drive.<\/p>\n<p>Well, isn\u2019t that a special little message? In reality, you must be in the context of your Configuration Manager site. This is simply an extension of the PSDrives in your Windows PowerShell console, and it is very much the same as changing a drive letter.<\/p>\n<p>The actual \u201cletter\u201d (if you want to think of it like that) is simply your site code. Now I could tell you, \u201cJust go look that up in the GUI.\u201d I could do that.<\/p>\n<p>But this is a Hey, Scripting Guys! Blog post, and it would make far more sense to show you how to get that information with Windows PowerShell, right?<\/p>\n<p>If you were to run <b>Get-PSDrive<\/b> on a system with the Configuration Manager console installed, you would get output similar to the following:<\/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\/50578.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\/50578.1.PNG\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>As you can see from the section highlighted in yellow, the Provider Type I use to target a Configuration Manager site is called <b>CMSite<\/b>. My site name is NIH (that\u2019s how I keep the users in control, you see\u2014every time I tell them the site name, they back away from the vicious sound of NIH).<\/p>\n<p>Too much Monty Python already? Sorry. I shall avoid saying NIH again. I promise. No more NIH.<\/p>\n<p>To programmatically access the site name, you should use an object like this in a PowerShell script that uses the cmdlets\u2014or perhaps add this to your profile if you regularly work with the Configuration Manager cmdlets:<\/p>\n<p style=\"margin-left: 30px;\">$CMSite=\u201d$(Get-PSDrive \u2013PSProvider CMSite)`:\u201d<\/p>\n<p style=\"margin-left: 30px;\">Set-Location $CMSite<\/p>\n<p>When you run this, you\u2019ll see a prompt similar to this on your console (you will see something other than NIH as your site name, followed by a colon):<\/p>\n<p style=\"margin-left: 30px;\">NIH:<\/p>\n<p>From this point we can directly ask Configuration Manager questions, such as, &#8220;What are the boundaries on this site?&#8221;<\/p>\n<p style=\"margin-left: 30px;\">Get-CMBoundary<\/p>\n<p>Or maybe something interesting like finding out the Configuration Manager site system server:<\/p>\n<p style=\"margin-left: 30px;\">Get-CMSiteSystemServer<\/p>\n<p>And of course, all of this information is easily exportable by using <b>Export-CSV<\/b> or <b>Export-CLIXML<\/b>. There are some amazing solutions on the Internet for documenting a Configuration Manager environment by using Windows PowerShell!<\/p>\n<p>SH, that is all there is to getting the Configuration Manager module loaded and getting access to its cmdlets. Stay tuned. Tomorrow I&#8217;ll explore how to use the Configuration Manager cmdlets to work with collections!<\/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>. 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<span style=\"font-size: 12px;\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Load the Configuration Manager cmdlets and make a basic site connection. \u00a0Hey, Scripting Guy! I\u2019ve been doing a lot of work in the Configuration Manager 2012 R2 console and it\u2019s beautiful. But I find I spend a lot of my time clicking all over the place and repeating tasks. Are there PowerShell cmdlets available [&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-74801","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: Load the Configuration Manager cmdlets and make a basic site connection. \u00a0Hey, Scripting Guy! I\u2019ve been doing a lot of work in the Configuration Manager 2012 R2 console and it\u2019s beautiful. But I find I spend a lot of my time clicking all over the place and repeating tasks. Are there PowerShell cmdlets available [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74801","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=74801"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74801\/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=74801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}