新的powershell,并试图找出如何格式化所有附加usb驱动器。下面的代码在格式期间提示输入驱动器字母,继续迭代,并且不会转义指定驱动器字母提示符。
foreach ($usbDrive in get-disk)
{
if ($usbDrive.bustype -eq 'usb')
{
Format-Volume -FileSystem FAT32
}
}这段代码似乎有效,但它提示输入的usb驱动器比插入的要多。如果可能的话,我希望驱动器保持相同的字母,并跳过提示。
foreach ($usbDrive in get-disk | where bustype -eq 'usb'){Format-Volume -FileSystem FAT32}发布于 2022-06-18 12:12:36
我今天正在做类似的事情,我用提示创建了这个脚本,以避免格式化任何不必要的驱动器:
$flashDrives = (get-volume | Where-Object { $_.drivetype -eq 'removable' })
foreach ($flashDrive in $flashDrives) {
$title = 'Format Drive?'
$message = 'Do you want to format ' + $flashDrive.FileSystemLabel + ' (' + $flashDrive.DriveLetter + ':) ' + 'with file system "' + $flashDrive.FileSystemType + '" ?'
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Format drive."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Skip drive."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result) {
0 { Format-Volume -DriveLetter $flashDrive.DriveLetter -FileSystem FAT32 }
1 { "Skipping drive" }
}
}如果您真的想在没有任何提示的情况下格式化,您可以删除这些提示,这是您自己的风险!
https://stackoverflow.com/questions/72575701
复制相似问题