[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vbscript to format USB drive and set label

Posted on 2013-05-20
13
Medium Priority
?
2,131 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 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