Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Introduction
When you work on a script, and want to format or create a new partition, volume or clean drive, you will be confronted with the annoying message prompt "You need to format the disk drive" or a new window explorer when your drive is created.
like these :
When you click on cancel displays another prompt stating that the drive is not accessible.
these errors occur because a letter is assigned to drive and sometimes it is not well formatted with RAW parition.
Solution :
some prefer to disable autoplay, others not to assign a letter to drive. there is no good solution for the script, we will see together how to deal with this problem with shellwindows object
**Step : **
- (facultative) format for exemple our drive without create a new partition
- create a partition or new volume withaout assign a letter, but chose a label
- assign letter to drive or partition, and then close explorer message box
About ShellWindows object
Represents a collection of the open windows that belong to the Shell. Methods associated with this objects can control and execute commands within the Shell, and obtain other Shell-related objects. you can find more information here Link
Create the variable :
$shell = New-Object -ComObject Shell.Application
Format your Drive :
Clear-Disk -Number $disk -RemoveData -RemoveOEM -Confirm:$false -PassThru<br>
Create your partition or your volume but d'ont forget to chose a label, in our exemple the label will be "CMCiso-source' :
New-Partition -DiskNumber $disk -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel 'CMCiso-Source'
Now we will assign a letter to our driver, dont forget to change partitionnumber with your partition, and variable $disk with number of you drive.
Add-PartitionAccessPath -DiskNumber $disk -PartitionNumber '1' -AssignDriveLetter -Confirm:$false
Just after we will close new window, or message box, for drive with label 'CMC'
foreach ($window in ($shell.Windows() | Where-Object { $_.LocationURL -like "$(([uri]"*CMCiso*").AbsoluteUri)*" }))
{
$window.Quit()
}
Conclusion
With this method, you will no longer have a windows message, to adapt it according to your needs, sometimes on old media USB, it will be interesting to add a sleep 2 seconds before the loop foreach.