Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Techmentor: FOR examples

Posted on March 21, 2006August 5, 2009

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) .

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Here are few of the examples I used in the Commandline Script session. (By the way, if you'd like to see more Command line stuff at future Techmentor shows or other scripting topics, be sure to let me or Don know.)

Assuming a text list of computer names, servers.txt and you want to do something to each computer, like ping it, you could run:

for /f %i in (servers.txt) do @ping %i

If you wanted to run cacls on a set of local directories you might try something like this:

for /f "tokens=*" %i in ('dir /b /ad') do @cacls %i

I use tokens to handle any directory names with spaces. If you wanted to send the results to a text file, use simple redirection. But use >> otherwise you'll only get the results from the last directory.

for /f "tokens=*" %i in ('dir /b /ad') do @cacls %i >>results.txt

One trick I use to do this for remote machines is to map a network drive to a file share then run cacls on the mapped drive.

Technorati Tags:
commandline
Scripting


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Techmentor: FOR examples”

  1. Andy says:
    May 22, 2006 at 7:13 pm

    You know with hindsight i wish i had mapped a remote drive to the local drive I was on when running cacls on a subdirectory on a server.
    I was doing something along the lines of
    for /D %i in (*) do cacls %i /r:everyone
    Now I was in the c:\documents and settings directory (and had a screenshot to prove it) but when I ran that command for some reason it decided to run it on c:\winnt….the end result really was not pretty.
    If I had mapped a drive to c:\documents and settings I would not have affected the rest of the server.

    BTW – the blog is excellent and very useful.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d