Solved

vbscript to format USB drive and set label

Posted on 2013-05-20
13
1,868 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

17 Experts available now in Live!

Get 1:1 Help Now