Solved

vbscript to format USB drive and set label

Posted on 2013-05-20
13
1,988 Views
Last Modified: 2013-05-21
Hi,

I need a simple vbs to format and set the drive label of a usb. I found this but it does not seem to do anything on Win8. Any suggestions please?
Tom

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colVolumes = objWMIService.ExecQuery _
("Select * from Win32_Volume Where Name = 'E:\\'")

For Each objVolume in colVolumes
errResult = objVolume.Format("NTFS", true)
objVolume.Label = "MyLabel"
Next

Open in new window

0
Comment
Question by:tomdenton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 18

Expert Comment

by:x-men
ID: 39180606
the script assumes drive E: for the usb. is this correct?
0
 

Author Comment

by:tomdenton
ID: 39180621
Yes but that can be changed to anything as needed
0
 
LVL 18

Expert Comment

by:x-men
ID: 39180630
what's returne on the errResult variable?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tomdenton
ID: 39180648
nothing seems to happen at all format wise and the labels is not changed either
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39180762
Try removing the double "\"...with this adjustment:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colVolumes = objWMIService.ExecQuery _
("Select * from Win32_Volume Where DriveType=3)

For Each objVolume in colVolumes
  If objVolume.Name = "E:\" Then  'Filters for ONLY drive E:
    errResult = objVolume.Format("NTFS", true)
    objVolume.Label = "MyLabel"
  End If
Next

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39180780
Also - are you running this in an elevated environment? (as Administrator)
0
 

Author Comment

by:tomdenton
ID: 39181425
yes i am full admin - (out now will test later)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39181466
But from an elevated environment?
For instance, launching your app using
cscript FormatUSB.vbs

Open in new window

in a command prompt...does the command window state that it's an Administrative environment?

See: http://www.eightforums.com/tutorials/2754-elevated-command-prompt-open-windows-8-a.html
0
 

Author Comment

by:tomdenton
ID: 39182623
Since the suggested change i get an 'unterminated string constant' error on line 6

and the same if run as cscript... from a cmd window

the command window does not specifically stat admin environment. I can however run lots of other scripts without a problem if that helps?!
0
 

Author Comment

by:tomdenton
ID: 39182651
i went back to my original script and ran that from an admin cmd and it ran and formatted the drive ok but did not rename its label

Will it be possible to make this work from a non admin point of view though?
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 39183882
Don't know of a way to do it without elevation.  You could create a shortcut and mark that to only run as admin, but you'll get that pop up asking you to confirm it each time.

Here, try this adjustment with the elevated cmd:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colVolumes = objWMIService.ExecQuery _
("Select * from Win32_Volume Where Name = 'E:\\'")

For Each objVolume in colVolumes
  errResult = objVolume.Format("NTFS", true,,"My Label")
Next

Open in new window

0
 

Author Closing Comment

by:tomdenton
ID: 39183892
This works fine if 'elevated'. The other option is i can compile it using exescript then right click and run as admin.

Thanks for your help
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39184028
Happy to help - thanx for the grade! :^)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question