{"id":66073,"date":"2006-11-09T13:28:00","date_gmt":"2006-11-09T13:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/11\/09\/how-can-i-use-windows-powershell-to-get-a-list-of-all-my-computers\/"},"modified":"2006-11-09T13:28:00","modified_gmt":"2006-11-09T13:28:00","slug":"how-can-i-use-windows-powershell-to-get-a-list-of-all-my-computers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-windows-powershell-to-get-a-list-of-all-my-computers\/","title":{"rendered":"How Can I Use Windows PowerShell to Get a List of All My Computers?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><\/p>\n<p>Using Windows PowerShell, how can I retrieve a list of all the computer accounts in my domain?<\/p>\n<p>&#8212; TE<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/a><\/p>\n<p>Hey, TE. You know, several years ago the Scripting Guy who writes this column was playing basketball with one of his brothers. Because there were a total of 5 people at the park they had to play 2-on-2, with one person forced to sit out. After a short while a sixth person showed up and asked if he could play. He was a little guy, very quiet and unassuming, but for some reason the Scripting Guy who writes this column took one look at him and thought, \u201cI bet he\u2019s good.\u201d<\/p>\n<p>\u201cI\u2019ll guard the new guy,\u201d said the Scripting Brother.<\/p>\n<p>\u201cI don\u2019t know,\u201d said the Scripting Guy who writes this column. \u201cMaybe we should play a zone or something.\u201d<\/p>\n<p>\u201cNo,\u201d said the Scripting Brother. \u201cI want to guard him.\u201d<\/p>\n<p>So how did it go? Let\u2019s put it this way: the Scripting Brother now knows, better than anyone, what the old saying about being careful what you wish for really means. As it turned out, the newcomer may very well have been the best basketball player in the history of the world. He drove past the Scripting Brother for easy lay-ins. He soared over the Scripting Brother to grab rebounds or bury jump shots. He completely and totally dominated both the game and the Scripting Brother.<\/p>\n<p>About halfway through the game the Scripting Brother turned to the Scripting Guy who writes this column and said, \u201cDo you want to switch guys?\u201d<\/p>\n<p>\u201cNo thanks,\u201d said the Scripting Guy who writes this column, who was guarding a guy that probably hadn\u2019t touched the ball more than 2 or 3 times the entire game. \u201cI\u2019m fine.\u201d<\/p>\n<p>Now, admittedly, writing a script that retrieves a list of computer accounts from Active Directory isn\u2019t quite as difficult as guarding an unguardable player in basketball. Nevertheless, we just wanted you to know what you\u2019re getting yourself into, TE. Remember, you asked for this:<\/p>\n<pre class=\"codeSample\">$strCategory = \"computer\"\n$objDomain = New-Object System.DirectoryServices.DirectoryEntry\n$objSearcher = New-Object System.DirectoryServices.DirectorySearcher\n$objSearcher.SearchRoot = $objDomain\n$objSearcher.Filter = (\"(objectCategory=$strCategory)\")\n$colProplist = \"name\"\nforeach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}\n$colResults = $objSearcher.FindAll()\nforeach ($objResult in $colResults)\n    {$objComputer = $objResult.Properties; $objComputer.name}\n<\/pre>\n<p>The truth is, this script is nowhere near as bad as it looks; the problem is that it <i>looks<\/i> pretty darn intimidating. But that\u2019s OK; after all, it <i>is<\/i> our job to try and explain how things like this work.<\/p>\n<p>With an emphasis on the word <i>try<\/i>, of course.<\/p>\n<p>We start out simply enough, assigning the value <i>computer<\/i> to a variable named $strCategory. We then use this line of code to create a new instance of the .NET Framework class <b>System.DirectoryServices.DirectoryEntry<\/b>. Because we didn\u2019t specify any additional parameters when creating this instance our object reference $objDomain will automatically connect us to the root of our Active Directory domain. Neat, huh?<\/p>\n<p>Incidentally, here\u2019s the line of code we were talking about:<\/p>\n<pre class=\"codeSample\">$objDomain = New-Object System.DirectoryServices.DirectoryEntry\n<\/pre>\n<p>After we connect to the domain we then need to create an instance of the <b>System.DirectoryServices.DirectorySearcher<\/b> class; that\u2019s the object we\u2019ll use to conduct our Active Directory search. After creating the object we then use these two lines of code to assign values to the DirectorySearcher <b>SearchRoot<\/b> and <b>Filter<\/b> properties:<\/p>\n<pre class=\"codeSample\">$objSearcher.SearchRoot = $objDomain\n$objSearcher.Filter = (\"(objectCategory=$strCategory)\")\n<\/pre>\n<p>As you probably figured out, the SearchRoot property simply tells DirectorySearcher where to begin searching. We want to search the entire domain so we set SearchRoot to the object reference $objDomain. In turn, the Filter property enables us to target a specific subset of the items found in Active Directory. Because the only thing we care about are computers, we tell the Filter property to bring back only those objects where the <b>objectCategory<\/b> attribute is equal to the variable $strCategory (which, as you recall, is equal to <i>computer<\/i>.)<\/p>\n<table class=\"dataTable\" id=\"EOE\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. A detailed explanation of how to search Active Directory using Windows PowerShell falls way outside the scope of this little column. We will note, however, that the Filter property requires the LDAP search syntax; you can\u2019t use the more familiar SQL-style syntax here. For more information, see the <a href=\"http:\/\/search.msdn.microsoft.com\/search\/Redirect.aspx?title=DirectorySearcher+Class+(System.DirectoryServices)+&amp;url=http:\/\/msdn2.microsoft.com\/en-us\/library\/system.directoryservices.directorysearcher.aspx\" target=\"_blank\"><b>DirectorySearcher documentation<\/b><\/a> on MSDN.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Our next step is to specify which attributes and attribute values we want our search to bring back. (If you don\u2019t specify a so-called \u201cproperty list\u201d you\u2019ll get back all the attributes and attribute values, which is not only more information than you need, but probably more data than you want to have clogging up your network.) These two lines of code add the <b>name<\/b> attribute to the list of property values we want back:<\/p>\n<pre class=\"codeSample\">$colProplist = \"name\"\nforeach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}\n<\/pre>\n<p>We should point out that property lists need to be constructed as arrays. Why do we care about that? Well, suppose we wanted to get back both the name and the cn. In that case we\u2019d have to assign the two values to $colProplist using syntax like this:<\/p>\n<pre class=\"codeSample\">$colProplist = \"name\", \"cn\"\n<\/pre>\n<p>And yes, that is an array: in Windows PowerShell you can create an array just by assigning multiple values (with individual values separated by commas) to a variable.<\/p>\n<p>After we\u2019ve created an array our next chore is to use the <b>PropertiesToLoad<\/b> method to add those properties to our DirectorySearcher. To do that we set up a For Each loop to loop through all the items in the array, adding each item to the DirectorySearcher:<\/p>\n<pre class=\"codeSample\">foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}\n<\/pre>\n<table class=\"dataTable\" id=\"EOF\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. What\u2019s that? You don\u2019t know how to create a For Each loop in Windows PowerShell? If that\u2019s the case, you should take a look at our <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/cmdlets\/foreach-object.mspx\"><b>Task-Based Introduction to Windows PowerShell<\/b><\/a>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Now, at long last, we\u2019re ready to search Active Directory. To do that we call the <b>FindAll()<\/b> method, storing the data the search brings back in a variable named $colResults:<\/p>\n<pre class=\"codeSample\">$colResults = $objSearcher.FindAll()\n<\/pre>\n<p>The only thing left to do now is set up a second For Each loop to loop through all the items (computers) in the collection $colResults. For each item (computer) in the collection we use this snippet of code to store the item <b>Properties<\/b> in a variable named $objComputer; we do that because, even though we asked for only the name, by default we also get back the computer\u2019s ADsPath, something which can make our output difficult to read:<\/p>\n<pre class=\"codeSample\">$objComputer = $objResult.Properties\n<\/pre>\n<p>If we don\u2019t mind difficult-to-read output we could just echo back the value of $colResults and call it good:<\/p>\n<pre class=\"codeSample\">$colResults\n<\/pre>\n<p>As soon as the computer Properties have been stashed in $objComputer we can then use this command to echo back just the computer name:<\/p>\n<pre class=\"codeSample\">$objComputer.name\n<\/pre>\n<p>That\u2019s going to give us output that looks something like this:<\/p>\n<pre class=\"codeSample\">atl-dc-01\natl-dc-02\natl-dc-03\natl-dc-04\n<\/pre>\n<p>Which is exactly what we wished for in the first place.<\/p>\n<p>Like we said, this script <i>is<\/i> a little complicated, although that\u2019s due in part to all the $\u2019s and curly braces and other unusual characters you have to use in a Windows PowerShell script. Complicated or not, though, we know that there\u2019s a lot of interest in using Windows PowerShell to work with Active Directory, so we\u2019ll see what we can do about that in the future.<\/p>\n<p>In the meantime, though, we\u2019re off to play some basketball. Hmmm, there\u2019s a quiet, unassuming little guy over there; we\u2019ll guard him. And no, we\u2019re not worried: don\u2019t they always say that you never have to be careful what you wish for, because things always turn out great? <\/p>\n<p>Or something like that anyway. We\u2019ll ask the Scripting Brother the next time we see him.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Windows PowerShell, how can I retrieve a list of all the computer accounts in my domain? &#8212; TE Hey, TE. You know, several years ago the Scripting Guy who writes this column was playing basketball with one of his brothers. Because there were a total of 5 people at the park they had to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,46,3,8,45],"class_list":["post-66073","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-computer-accounts","tag-scripting-guy","tag-searching-active-directory","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Using Windows PowerShell, how can I retrieve a list of all the computer accounts in my domain? &#8212; TE Hey, TE. You know, several years ago the Scripting Guy who writes this column was playing basketball with one of his brothers. Because there were a total of 5 people at the park they had to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66073","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66073"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66073\/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=66073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}