Solved

vbscript to format USB drive and set label

Posted on 2013-05-20
13
1,843 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now