Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


January 2006

Harness the Power of the Command Line

Use batch files to perform administrative tasks (and save time, too)
RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

My career as an IT professional has been filled with reminders that almost everyone I interact with is much younger than I. One advantage I gain from my age and my long history with computers is an automatic reflex to use command-based tools whenever I have to perform a task. Often, when I interact with administrators who are struggling to create complicated scripts, I can't resist saying, "A batch file would be easier, quicker, and work just as well." I get some strange looks, because so many administrators are too young to have had experience with DOS, and they aren't quite sure what a batch file is. (I tell them a batch file is simply a text file containing DOS commands.) Whenever I write about batch files, I get a ton of email asking for more information about creating and using these handy utilities.

Mapping Drives from the Command Line
Most of the queries I receive from administrators ask about mapping drives throughout the network to make sure users access the appropriate folders on remote computers. (These admins often add that they try to script the solution but find the task more work than they anticipated.) They also want to know if there's a secret to mapping a drive to a folder that isn't shared. The answer to both questions is to use the command line.

You can map drives from the command line with the Net Use command. Type

net use 

with no switches at a command prompt and you'll receive information about the currently mapped drives that also lets you know which drive letters are available. The following information displays:

  • The current state of the Reconnect at Logon option, specified either by the text "New Connections Will Be Remembered" or "New Connections Will Not Be Remembered."
  • The current status of each connection, specified either by OK (which means connected) or Disconnected.
  • The drive letter.
  • The mapped folder's name in Universal Naming Convention (UNC) format.
  • The network type (usually Microsoft Windows, but if you're running Novell NetWare client software and mapping drives to a NetWare server, the network listing is NetWare).

Usually, mapped drives are marked Disconnected when you first boot the computer, but as soon as you access the mapped drive its status automatically changes to Connected. You can access the mapped drive in My Computer or by entering the drive letter in the command window, as follows:

k: 

Don't forget the colon.

Creating a Mapped Drive
To map a shared folder to a drive letter, enter on the command line

net use d: \\<computername>\<sharename> 

Substitute the drive letter you want to assign to the share for d. If any folder in the path has a name that includes a space, enclose the entire path, including the opening double backslash, in quotation marks. Windows returns the message The Command Completed Successfully. If you're a skeptic, use the Net Use command to make sure your new mapped drive appears in the list the command returns.

Whether or not your new mapping reconnects at logon depends on the current state of the Reconnect at Logon option. You can set the reconnection option you prefer with the /persistent: [yes/no] switch. For example, to reconnect at logon, enter

net use f: \\<computer\share>/persistent: yes 

To map a drive for the current session only, enter

net use f: \\<computer\share> /persistent: no 

The reconnection option you select becomes the default state of the Reconnect at Logon option (including the option displayed on the GUI version of the Map Network Drive dialog).

Mapping Drives to Unshared Folders
Unlike the mapping function in the Windows GUI, the Net Use command doesn't limit you to mapping shared folders. To map an unshared subfolder of a shared folder or any folder on a shared drive, enter on the command line

net use f: 
  \\<computer>\<sharename>\<subfoldername> 

The ability to map a resource that isn't explicitly shared is useful because you can share a drive, then map any or all of the folders on that drive. Of course, you must be judicious about security in such an environment, so make sure the permissions in the target unshared folder are set appropriately.

Removing a Mapped Drive
To remove a mapped drive, enter

net use <d>: /delete 

substituting the appropriate drive letter for d. The system returns the message

d: was deleted successfully 

Batch Files for Mapping Drives
The Net Use command gives you the ability to use batch files to create mapped drives. Administrators can use logon batch files that contain the Net Use command to set mapped drives for users. Mapped drives make it easy for users to access the appropriate folders for specific software and data. For example, for users who work in the company database, you might have a logon script with the following contents:

net use g: \\<DBServer<\<OurDatabase> 
net use h: \\<DBServer>\<DatabaseFiles> 

Name the file whatever you want (it must have a .bat extension), and copy it to the logon script directory on your domain controller (DC). The default location for logon scripts on a DC is %SystemRoot%\ SYSVOL\sysvol\<domainname.com>\scripts. The scripts subfolder is automatically shared with the share name NETLOGON (which is the name that appears on remote computers when you expand the DC in My Network Places).

Be careful not to map the drive letter that's in use for User Home Drives. During user logon, Windows maps the home drive first, before running any logon scripts from the scripts subfolder. If your logon script tries to map a drive letter that's already used, the system returns the error The local device name is already in use.

   Previous  [1]  2  3  4  Next 


Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...

New Microsoft/Yahoo! Deal? No

On Sunday, the Times of London reported that Microsoft had renewed talks with failing Internet giant Yahoo! and would manage its search engine for 10 years, while Yahoo! would retain control of its email, messaging, and content services. This report ...


Related Events SQL Server 2008 – Can You Wait? | Philadelphia

SQL Server 2008 – Can You Wait? | Atlanta

SQL Server 2008 – Can You Wait? | Chicago

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing