{"id":4762,"date":"2012-10-27T00:01:00","date_gmt":"2012-10-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/27\/weekend-scripter-getting-directory-listings-from-remote-servers\/"},"modified":"2012-10-27T00:01:00","modified_gmt":"2012-10-27T00:01:00","slug":"weekend-scripter-getting-directory-listings-from-remote-servers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-getting-directory-listings-from-remote-servers\/","title":{"rendered":"Weekend Scripter: Getting Directory Listings from Remote Servers"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to get a directory listing from remote servers by using Windows PowerShell and how to write results to an CSV file.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is here: Windows PowerShell Saturday #003 in Atlanta, Georgia (Alpharetta). The speaker&rsquo;s dinner last night was great, and it gave the Scripting Wife a chance to meet new friends and to renew acquaintances with people we rarely get to see. Overall, it was great for everyone except me as I am still at home with no voice to speak of and a stuffy head.Today, more than 100 people will gather and spend the day listening to world-class speakers talking about Windows PowerShell. It will be fun.<\/p>\n<h2>Getting a directory listing from a group of servers<\/h2>\n<p>The other day, a follower of @ScriptingGuys on Twitter asked me about getting a directory listing from a bunch of servers (like over a hundred) and writing the results to an Excel Spreadsheet. There are three tasks here.<\/p>\n<ol>\n<li>Obtain a listing of the servers to query.<\/li>\n<li>Obtain the directory listing from the servers.<\/li>\n<li>Write results to an Excel spreadsheet.<\/li>\n<\/ol>\n<h2>Obtain a listing of servers to query<\/h2>\n<p>There are numerous ways to obtain a listing of servers. The different methodologies appear here.<\/p>\n<ol>\n<li>Read a Text file (or a CSV file) containing a list of all the servers.<\/li>\n<li>Read a SQL Server database (or an Excel spreadsheet) containing a list of all the servers.<\/li>\n<li>Use ADO to query Active Directory to obtain a list of all the servers.<\/li>\n<li>Use the [ADSISearcher] type accelerator to query Active Directory and obtain a list of all the servers.<\/li>\n<li>Use a Windows PowerShell cmdlet to query Active Directory.<\/li>\n<\/ol>\n<p>I have talked about all of these technologies in previous years. You can use the search function or the blog tags to find these articles. Actually, each of the approaches has advantages and disadvantages. Reading a text file is very simple, but then, so is using a Windows PowerShell cmdlet to query Active Directory. The problem with a text file is that you have to always have access to it, and you also have to maintain it. With Active Directory, it will always be up to date, and maintenance is not, in fact, an issue. Most of the time, nowadays, I tend to query Active Directory. It is fast, easy, and accurate. The easiest way to get a computer listing is to use the <b>Get-ADComputer<\/b> cmdlet from the AD&nbsp;DS Windows PowerShell tools. For my Windows&nbsp;8 laptop, <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=28972\" target=\"_blank\">I downloaded them from this link<\/a>. Keep in mind that the RSAT tools are both operating system-specific as well as processor-specific.<\/p>\n<p><b>Note<\/b>&nbsp; &nbsp;I have <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=rsat&amp;sections=7618\" target=\"_blank\">written a number of articles that deal with the RSAT tools for Windows 7<\/a>. I have not yet written about the RSAT tools for Windows&nbsp;8.<\/p>\n<p>The first thing I do is import the ActiveDirectory module. I use the command appearing here (<b>ipmo<\/b> is the alias for <b>Import-Module<\/b>, and *active* is a wild card pattern that finds the ActiveDirectory module).<\/p>\n<p style=\"padding-left: 30px\">ipmo *active*<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp; &nbsp;In Windows PowerShell 3.0, you do not need to import a module prior to using it, but if I know that I am going to use a module, I go ahead and import it, because it is faster.<\/p>\n<p>Next, I use the <b>Get-ADComputer<\/b> cmdlet and use a filter that returns everything. I store the results in a $cn variable. This command appears here.<\/p>\n<p style=\"padding-left: 30px\">$cn = Get-ADComputer -Filter *<\/p>\n<h2>Obtaining the directory listings<\/h2>\n<p>Now, I create a credential object to permit me to run a remote command on the remote servers. To do this, I use the <b>Get-Credential<\/b> cmdlet as shown here.<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential -Credential iammred\\administrator<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b> &nbsp;&nbsp;The account I am using does not have admin rights on the remote computers, but it does have permission to query Active Directory. If your account does not have rights to query Active Directory, you would need to use the credentials earlier when you query Active Directory. The command would be something like this: Get-ADComputer -Filter * -Credential $cred<\/p>\n<p>Once I have a credential object, I need to use the <b>Invoke-Command<\/b> cmdlet to run the command on the remote servers. This command appears here (<b>icm<\/b> is an alias for the <b>Invoke-Command<\/b> cmdlet).<\/p>\n<p style=\"padding-left: 30px\">icm -Credential $cred -Cn $cn.name -ScriptBlock {dir}<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp;&nbsp; This command takes advantage of Windows PowerShell&nbsp;3.0 automatic enumeration of collections that permit me to access a single property from a collection of objects. If you are not using Windows PowerShell&nbsp;3.0, you will need to do something like this: $a = $cn | foreach {$_.name}<\/p>\n<h2>Write to a CSV file<\/h2>\n<p>To write to a CSV file, I use the <b>Export-CSV<\/b> cmdlet and specify a local path. Because I am not using a shared storage mechanism, but rather using a CSV file on my local system, I add the <b>Export-CSV<\/b> command after the <b>Invoke-Command<\/b> cmdlet and use a local path. The command appears here.<\/p>\n<p style=\"padding-left: 30px\">| export-csv -path c:\\fso\\serverDir.csv &ndash;Append<\/p>\n<h2>Putting it all together<\/h2>\n<p>The complete sequence of commands is shown here.<\/p>\n<p style=\"padding-left: 30px\">ipmo *active*<\/p>\n<p style=\"padding-left: 30px\">$cn = Get-ADComputer -Filter *<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential -Credential iammred\\administrator<\/p>\n<p style=\"padding-left: 30px\">icm -Credential $cred -Cn $cn.name -ScriptBlock {dir} | export-csv -path c:\\fso\\serverDir.csv &ndash;Append<\/p>\n<p>Four commands. That is it. Now, I did not do any error checking, and, as a result, I got a bunch of errors in the Windows PowerShell console when I ran the command. This was due to servers being offline on my home network. Here are the errors.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0702.hsg-10-27-12-01.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0702.hsg-10-27-12-01.png\" \/><\/a><\/p>\n<p>When I double-click on the CSV file, it opens automatically in Microsoft Excel. This is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1447.hsg-10-27-12-02.png\"><img decoding=\"async\" title=\"Image of Excel output\" alt=\"Image of Excel output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1447.hsg-10-27-12-02.png\" \/><\/a><\/p>\n<p>One thing to notice is that I did not make any changes to the output. If I am not interested in all this information, I can use the <b>Select-Object<\/b> cmdlet to choose what to write to the CSV file.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp;&nbsp; I have <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=PowerShell%20CSV&amp;sections=7618\" target=\"_blank\">written quite a bit about working with CSV files<\/a> from within Windows PowerShell.<\/p>\n<p>That should get you started collecting directory inventories from your servers. Have a great weekend, and I will see you tomorrow.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to get a directory listing from remote servers by using Windows PowerShell and how to write results to an CSV file. Microsoft Scripting Guy, Ed Wilson, is here. It is here: Windows PowerShell Saturday #003 in Atlanta, Georgia (Alpharetta). The speaker&rsquo;s dinner last night was great, and [&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":[7,3,303,61,45],"class_list":["post-4762","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-searching","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to get a directory listing from remote servers by using Windows PowerShell and how to write results to an CSV file. Microsoft Scripting Guy, Ed Wilson, is here. It is here: Windows PowerShell Saturday #003 in Atlanta, Georgia (Alpharetta). The speaker&rsquo;s dinner last night was great, and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4762","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=4762"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4762\/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=4762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}