Techmentor: FOR examples

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 (grab the FOR Essentials link) .

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:

Techmentor: DSQuery and DSMod examples

During the command line scripting session, I demonstrated how to use the directory service command line tools like dsmod, dsquery and dsget. You can get syntax help by running ‘dsquery /?’ (or dsmod,dsadd,dsget). There’s a lot of help information so you’ll probably want to pipe the results using More (dsquery /? | more) You can also get help on each subcommand by using ‘dsmod user /?’

Here’s my list of demos. You can change the distinguished name of the starting path to match a container or domain root in your network. The character after -limit is a zero.

display all users in the domain by DN:
dsquery user -limit 0

display all users in the sales ou by samid
dsquery user ou=sales,dc=matrix,dc=local -limit 0 -o samid

query all users in the sales ou by dn and get their display name and dept
dsquery user ou=sales,dc=matrix,dc=local -limit 0 -o dn|dsget user -display -dept

query all users in the sales ou by dn and set their department
dsquery user ou=sales,dc=matrix,dc=local -limit 0 -o dn|dsmod user -dept “Sales”

Technorati Tags:

Get Percent Free Space

I’ve put together a function that will return the percentage free space on a given logical drive for a specified server. The function in the following script is really doing all the hard work. The rest of the script is simply a demonstration on how to call the function. The script uses WMI and queries the Win32_LogicalDisk for the size and freespace properties. With these values it’s pretty simple to calculate a percentage.


specify alternate credentials for remote systems

strSrv=InputBox(“What computer do you want to query”,”Get Percent Free”,”localhost”)

strDeviceID=InputBox(“What drive do you want to query?”,”Get Percent Free”,”c:”)

WScript.Echo UCase(strDeviceID) & ” on ” & UCase(strSrv) & ” has ” & GetPercent(strSrv,strDeviceID,strUsername,strPassword) & ” free space.”

Function GetPercent(strSrv,strDeviceID,strUsername,strPassword)
‘deviceID is the drive letter and colon, eg E: with no trailing \

On Error Resume Next
Dim SWBemlocator,objWMI,objRef
Const wbemFlagReturnImmediately=&h10
Const wbemFlagForwardOnly=&h20

strQuery=”Select DeviceID,Size,FreeSpace from Win32_LogicalDisk WHERE DeviceID='” & strDeviceID & “‘”

‘strip out any trailing \ if found
If Right(strDeviceID,1)=”\” Then strDeviceID=Replace(strDeviceID,”\”,””)

‘validate strDeviceID
If Len(strDeviceID)=2 And Right(strDeviceID,1)=”:” Then

Set SWBemlocator = CreateObject(“WbemScripting.SWbemLocator”)
Set objWMI=SWBemlocator.ConnectServer(strSrv,”\root\CIMV2″,strUsername,strPassword)

Set objRef=objWMI.ExecQuery(strQuery,”WQL”,wbemForwardOnly+wbemFlagReturnImmediately)

For Each dev In objRef

If iSize=”” Then

GetPercent=”Drive Not Found”
End If


End If

End Function

Technorati Tags:

Chapter 5 Errata

Chapter 5 in Advanced VBScript for Microsoft Windows Administrators has some minor misinformation. The script in Listing 5-7 doesn’t work as you might expect it to. A zip file with corrected information and listing is available at

If you uncover other issues or have questions or comments, please feel free to email me or post in the forums at

Advanced VBScript is a Bookpool Best Seller

Thanks to all of you, Advanced VBScript for Microsoft Windows Administrators broke the 10 ten on‘s new releases best seller list. There is also a very nice review of the book at Amazon. We’d truly appreciate honest reviews at your favorite online bookseller.

Some of you have asked about autographed copies, and that is in the works. I’ll let you know more when that is set up. If you are at Techmentor next month, we’ll be happy to sign books.

Don’t forget, you can get a free sample chapter at