【问题标题】:Format multiple USB drives in powershell在PowerShell中格式化多个USB驱动器
【发布时间】:2022-06-18 20:12:36
【问题描述】:

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}

【问题讨论】:

  • 你没有向命令本身传递任何东西。

标签: powershell usb-drive


【解决方案1】:

我今天正在做类似的事情,我创建了这个脚本并提示避免格式化任何不需要的驱动器:

$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" }
    }
}

如果您真的想在没有任何提示的情况下进行格式化,您可以删除提示,风险自负!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    相关资源
    最近更新 更多