{"id":20,"date":"2006-02-10T08:59:00","date_gmt":"2006-02-10T12:59:00","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/2006\/02\/10\/cli-101-for\/"},"modified":"2009-08-05T13:03:54","modified_gmt":"2009-08-05T17:03:54","slug":"cli-101-for","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/","title":{"rendered":"CLI 101 &#8211; FOR"},"content":{"rendered":"<p>I've long maintained that the FOR command is one of most basic commands every administrator should know.  I have a short tutorial you can download at <a href=\"http:\/\/www.jdhitsolutions.com\/tutorials.htm\">http:\/\/www.jdhitsolutions.com\/tutorials.htm<\/a><\/p>\n<p>Here are some other examples on using the FOR command.  Let's say you have some command line utility that will take a computer name as a parameter, such as ping, and you want to run it against a list of computers.  List the computer names in a text file like servers.txt.  Then open a command prompt where the file is and run:<\/p>\n<p><span style=\"font-family:courier new;\">FOR \/F %x in (servers.txt) do @ping -n 1 %x<\/span><\/p>\n<p>If you want to verify you have the basic syntax correct, run this:<\/p>\n<p><span style=\"font-family:courier new;\">FOR \/F %x in (servers.txt) do @Echo %x<\/span><\/p>\n<p>This should display each server name in the list.<\/p>\n<p>If you want to save the output to a text file for any command you can use console redirection, like this:<\/p>\n<p><span style=\"font-family:courier new;\">FOR \/F %x in (servers.txt) do @ping -n 1 %x >>pingresults.txt<\/span><\/p>\n<p>Be sure to use >> and not > or you will only get results for the last computer in the text file.<\/p>\n<p>If you want to take the FOR command and put it in a batch file, then remember to use %% instead of %.  Here's a quick batch file version of the ping command example.  To avoid errors, I recommend specifying the full path to the text file.<\/p>\n<p><span style=\"font-size:78%;\"><span style=\"font-family:courier new;\">@echo off<br \/><\/span><span style=\"font-family:courier new;\">::PINGCHECK.BAT<br \/><\/span><span style=\"font-family:courier new;\">REM Delete pingresults.txt if it already exists so we get a new log<\/span><br \/><span style=\"font-family:courier new;\">if Exist c:\\scripts\\pingresults.txt DEL c:\\scripts\\pingresults.txt<\/span><br \/><span style=\"font-family:courier new;\">FOR \/F %%x in (c:\\scripts\\servers.txt) do @ping -n 1 %%x >>c:\\scripts\\pingresults.txt<\/span><br \/><span style=\"font-family:courier new;\">::END OF SCRIPT<\/span><\/span><\/p>\n<p>IMPORTANT: The FOR command is only case sensitive when it comes to variables. %x is different than %X.  If you run:<\/p>\n<p><span style=\"font-family:courier new;\">FOR \/F %x in (servers.txt) do @Echo %X<\/span><\/p>\n<p>You will not get the servers but rather %X.<\/p>\n<p><font face=\"tahoma\" size=1>Technorati Tags:<br \/><a href=\"http:\/\/www.technorati.com\/tags\/commandline\" rel=\"tag\">commandline<\/a><br \/><a href=\"http:\/\/www.technoratic.com\/tags\/scripting\" rel=\"tag\">Scripting<\/a><br \/><a href=\"http:\/\/www.technoratic.com\/tags\/batch file\" rel=\"tag\">batch file<\/a><br \/><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve long maintained that the FOR command is one of most basic commands every administrator should know. I have a short tutorial you can download at http:\/\/www.jdhitsolutions.com\/tutorials.htm Here are some other examples on using the FOR command. Let&#8217;s say you have some command line utility that will take a computer name as a parameter, such&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[8],"tags":[20],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-scripting","tag-cli"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>CLI 101 - FOR &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CLI 101 - FOR &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"I&#039;ve long maintained that the FOR command is one of most basic commands every administrator should know. I have a short tutorial you can download at http:\/\/www.jdhitsolutions.com\/tutorials.htm Here are some other examples on using the FOR command. Let&#039;s say you have some command line utility that will take a computer name as a parameter, such...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2006-02-10T12:59:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2009-08-05T17:03:54+00:00\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/20\\\/cli-101-for\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/20\\\/cli-101-for\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"CLI 101 &#8211; FOR\",\"datePublished\":\"2006-02-10T12:59:00+00:00\",\"dateModified\":\"2009-08-05T17:03:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/20\\\/cli-101-for\\\/\"},\"wordCount\":333,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"keywords\":[\"CLI\"],\"articleSection\":[\"Scripting\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/20\\\/cli-101-for\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/20\\\/cli-101-for\\\/\",\"name\":\"CLI 101 - FOR &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"datePublished\":\"2006-02-10T12:59:00+00:00\",\"dateModified\":\"2009-08-05T17:03:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/20\\\/cli-101-for\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/20\\\/cli-101-for\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/scripting\\\/20\\\/cli-101-for\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Scripting\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/scripting\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CLI 101 &#8211; FOR\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"CLI 101 - FOR &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/","og_locale":"en_US","og_type":"article","og_title":"CLI 101 - FOR &#8226; The Lonely Administrator","og_description":"I've long maintained that the FOR command is one of most basic commands every administrator should know. I have a short tutorial you can download at http:\/\/www.jdhitsolutions.com\/tutorials.htm Here are some other examples on using the FOR command. Let's say you have some command line utility that will take a computer name as a parameter, such...","og_url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/","og_site_name":"The Lonely Administrator","article_published_time":"2006-02-10T12:59:00+00:00","article_modified_time":"2009-08-05T17:03:54+00:00","author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"CLI 101 &#8211; FOR","datePublished":"2006-02-10T12:59:00+00:00","dateModified":"2009-08-05T17:03:54+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/"},"wordCount":333,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"keywords":["CLI"],"articleSection":["Scripting"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/","url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/","name":"CLI 101 - FOR &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"datePublished":"2006-02-10T12:59:00+00:00","dateModified":"2009-08-05T17:03:54+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/scripting\/20\/cli-101-for\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Scripting","item":"https:\/\/jdhitsolutions.com\/blog\/category\/scripting\/"},{"@type":"ListItem","position":2,"name":"CLI 101 &#8211; FOR"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":26,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/26\/techmentor-for-examples\/","url_meta":{"origin":20,"position":0},"title":"Techmentor: FOR examples","author":"Jeffery Hicks","date":"March 21, 2006","format":false,"excerpt":"The FOR command is one of the most important commands you can use as a Windows administrator. I have a short article on the command you can download at http:\/\/www.jdhitsolutions.com\/tutorials.htm (grab the FOR Essentials link) . Here are few of the examples I used in the Commandline Script session. (By\u2026","rel":"","context":"In &quot;Scripting&quot;","block_context":{"text":"Scripting","link":"https:\/\/jdhitsolutions.com\/blog\/category\/scripting\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/27\/techmentor-wmic-examples\/","url_meta":{"origin":20,"position":1},"title":"Techmentor: WMIC Examples","author":"Jeffery Hicks","date":"March 21, 2006","format":false,"excerpt":"I didn't get a chance to run through the WMIC examples. The Advanced VBScript book covers this topic in a little more detail. I also have a short PDF intro on WMIC you can download at http:\/\/www.jdhitsolutions.com\/tutorials.htmBefore you run any examples, you need to \"install\" WMIC. This basically means at\u2026","rel":"","context":"In &quot;Scripting&quot;","block_context":{"text":"Scripting","link":"https:\/\/jdhitsolutions.com\/blog\/category\/scripting\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":88,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/88\/powershell-parsing\/","url_meta":{"origin":20,"position":2},"title":"Powershell Parsing","author":"Jeffery Hicks","date":"January 16, 2007","format":false,"excerpt":"In PowerShell, Get-WMIObject is a terrific cmdlet for remotely managing systems. If you have a text list of server or computer names, here's a quick method you could enumerate that list and do something to each server.foreach ($server in (Get-Content s:\\servers.txt)) {#skip blank lines if (($server).length -gt 0) { $server\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1708,"url":"https:\/\/jdhitsolutions.com\/blog\/commandline\/1708\/turning-cli-tools-into-powershell-tools-deep-dive-demos\/","url_meta":{"origin":20,"position":3},"title":"Turning CLI Tools into PowerShell Tools Deep Dive Demos","author":"Jeffery Hicks","date":"October 24, 2011","format":false,"excerpt":"Last week I did a presentation at the PowerShell Deep Dive in Frankfurt about turning command line tools into PowerShell tools. A video recording should be posted later. But in the meantime, here is a copy of my slide deck, in PDF and a zip file with my demos and\u2026","rel":"","context":"In &quot;CommandLine&quot;","block_context":{"text":"CommandLine","link":"https:\/\/jdhitsolutions.com\/blog\/category\/commandline\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/nbtstat-n-300x158.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":140,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/140\/more-cli-one-liners\/","url_meta":{"origin":20,"position":4},"title":"More CLI One-Liners","author":"Jeffery Hicks","date":"May 6, 2008","format":false,"excerpt":"In the past I've posted a collection of command one-liners that get a ton of work done with (relatively) minimal effort. Many of these have come from Wayne Martin. I've been behind in my blogging while book writing, but I wanted to make sure you checked out his latest list\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":124,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/124\/explore-your-world\/","url_meta":{"origin":20,"position":5},"title":"Explore Your World","author":"Jeffery Hicks","date":"December 11, 2007","format":false,"excerpt":"This week's Windows Tip Sheet column is about opening an Explorer window from the command prompt. One of my readers sent me an email about using this tip in Vista: I\u2019m currently running Vista and when I type \u201cexplorer.exe \/e, %cd%\u201d I get the same results as \u201cexplorer.exe \/e \/root,\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}