diskpart drive if certain data exist

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Gaylord Haynes Gaylord Haynes 8 months, 1 week ago.

  • Author
    Posts
  • #36967
    Profile photo of Gaylord Haynes
    Gaylord Haynes
    Participant

    I am trying to build a powershell script to run diskpart, select disk#, then clean selected disk only when [System.IO.Directory]::Exists("$("$(($partition.DeviceID)\Windows\System32")") -and (Test-Path -Path "$("$($partition.DeviceID)\Windows\System32")")) and $_bustype -ne "USB"

    Any help would be greatly appreciated.

  • #36968
    Profile photo of random commandline
    random commandline
    Participant
  • #36974
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    Here is a script I use to format USB drives. You can add your file check and set the Drive type to "3" for physical disks.

    $USBDrive = $USBDrive = Read-Host "Enter the drive letter of the usb device that you would like to load:"
    $CheckUSB = gwmi -Query "Select DeviceID From Win32_LogicalDisk Where DeviceID = '$USBDrive' and DriveType = 2"
    If(!$CheckUSB)
    {
    	Write-Host " The Drive letter entered is not a USB device or the Drive was entered incorrectly!"
        exit
    }
    Write-Host "Press any key to format the drive and copy the files. Use Ctrl+C to cancel." -ForegroundColor Red -BackgroundColor White
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")
    $command = @"
    select Volume $USBDrive
    Clean
    create partition primary
    select partition 1
    active
    format fs=ntfs quick
    active
    exit
    "@
    $command|diskpart
    
    If($LASTEXITCODE -gt 0)
    {
    	Write-host "The format of $USBDrive failed!! Check the drive and try again!"
    	exit
    }
    Else
    {
    	Write-Host "The format of $USBDrive is complete"
    }
    
  • #36986
    Profile photo of Gaylord Haynes
    Gaylord Haynes
    Participant

    Jonathan, thank you for sharing the above code with me that would definitely come in handy. I am trying to call the query the drives discovered in the system and locate the drive that has $Partition.DeviceID\windows\system32 and then allow me to run diskpart to clean and format on that drive automatically based on if the folder exist on the drive.

    Any help would be greatly appreciated it has been kicking my butt for a couple days now trying to figure out how to properly query win32_Logicaldisk to make that happen.

    Thanks,

  • #36987
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    To start with a query to get of of the disks on the system would be

    Get-WmiObject -Query "Select * From Win32_LogicalDisk"

    You will need to limit the list to drives that you can format. The DriveType from the query will identify what type of drive and will have one of the following values:
    `
    Unknown (0)
    No Root Directory (1)
    Removable Disk (2)
    Local Disk (3)
    Network Drive (4)
    Compact Disc (5)
    RAM Disk (6)
    `
    See https://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx#properties for all the wonderful info available from win32_Logicaldisk.
    Now you need to choose what disks you are going to search so we will update the query return just the information we need. For just USB drives it would be

    Get-WmiObject -Query "Select * From Win32_LogicalDisk Where DriveType = '2'"|select DeviceID, DriveType

    For just normal disks it would be

    Get-WmiObject -Query "Select * From Win32_LogicalDisk Where DriveType = '3' "|select DeviceID, DriveType

    And for Both it would be

    Get-WmiObject -Query "Select * From Win32_LogicalDisk Where DriveType = '2' or DriveType = '3' "|select DeviceID, DriveType

    For this example I am going to use the Removable drive type of "2" so anyone who might copy and paste later code will not accidentally format a system unintentionaly. (I may or may not have done the same thing numerous times testing these types of scripts.) But you can set the query to what meets your needs.
    On to the script

    $drives = Get-WmiObject -Query "Select * From Win32_LogicalDisk Where DriveType = '2'"|select DeviceID, DriveType
    $drives
    

    Which gives a return on my test system of

    DeviceID DriveType
    -------- ---------
    G:               2
    H:               2
    

    Now you want to look for the windows\system32 directory. Now the script looks like this

    $drives = Get-WmiObject -Query "Select * From Win32_LogicalDisk Where DriveType = '2'"|select DeviceID, DriveType
    Foreach($drive in $drives){
        Write-Output "Checking $($drive.DeviceID) for \Windows\System32 "
        Test-Path -Path "$($drive.DeviceID)\Windows\System32"
    }
    

    And on my test system one drive was found

    Checking G: for \Windows\System32 
    False
    Checking H: for \Windows\System32 
    True
    

    Finally once you have identified the drive you can format it

    $drives = Get-WmiObject -Query "Select DeviceID From Win32_LogicalDisk Where DriveType = '2'"
    Foreach($drive in $drives){
        $drive = $drive.DeviceID
        Write-Output "Checking $drive for \Windows\System32 "
        If(Test-Path -Path "$drive\Windows\System32"){
            $command = @"
            select Volume $drive
            Clean
            create partition primary
            select partition 1
            active
            format fs=ntfs quick
            active
            exit
    "@
            $command|diskpart
    
            If($LASTEXITCODE -gt 0)
            {
    	        Write-Output "The format of $drive failed!! Check the drive and try again!"
    	        exit
            }
            Else
            {
    	        Write-Output "The format of $drive is complete"
            }
    
        }
    }
    

    You can adjust the diskpart commands to match the config you want to apply.
    To run this script you must be a local admin and I strongly suggest you add additional check to ensure you do not format something you do not intend to.

  • #36996
    Profile photo of Gaylord Haynes
    Gaylord Haynes
    Participant

    Thank you so much for your help. I was able to use your example modify my script to include your call and it now works perfectly. Thanks again.

  • #37192
    Profile photo of Gaylord Haynes
    Gaylord Haynes
    Participant

    Hi Jason,
    The script above is working beautifully. I have build a script that includes runs in a form. I would like to tie the script above to a progress bar at the bottom of the form. That starts when the button "Diskpart" is pressed, run the DISKPART script above and end when the script is finish. Instead of opening up a powershell window I would like it to run at the bottom on the same form. Or pop up a GUI Progress bar then close after complete. I am not to familiar with the progress bar function. Any help would be great.

    Thanks Again,

You must be logged in to reply to this topic.