none
Script for listing all drives on a PC RRS feed

  • Question

  • Hi,

    I have to create a batch script that shrinks drive C: and after that with diskpart to assign drive letter D: and to format the newly created disk. The script is about to be executed on 1000+ workstations, some of them have only drive C:, the rest of them have D: - Local Disk, D: - CD-ROM. The idea is that the script first has to perform check:

    1. If D: is local disk - skip and go to end.

    2. If D: is a CD-ROM - start sub script that renames D: to E: and after that launch the main script to shrink C: and assign drive D: to the shrinked part, assign drive letter D:.

    3. If drive D: is not present - launch the main script.

    I have tried to create the part of the script that checks for all drives:

    @echo off

    for /F "tokens=1,2 delims=*" %%a in ('wmic logicaldisk get caption^,description ^| findstr "D:"') do set drive=%%a

    echo %drive%

    However, the script returns the disk drive, but I do not know how to process the output so that to use it in the above mentioned three cases. I do not have any experience in batch scripting and I am not completely sure that my approach with wmic is the correct one for that purpose. Does anybody know how the script has to look like?


    • Edited by Anton Vasilev Thursday, February 15, 2018 9:36 AM
    • Moved by Bill_Stewart Monday, April 30, 2018 9:11 PM This is not "scripts on demand"
    Thursday, February 15, 2018 9:35 AM

All replies

  • Batch is obsolete.  Use PowerShell for all scripting now.

    We cannot write a scrip for you and we cannot teach you how to use Windows tools.  Look at the help for diskpart to see how to use it.

    You have to at least attempt to learn the technology you wish to work with,

    Please carefully review the following links to set your expectation of technical forums.

    This Forum is for Scripting Question Rather than script requests

    Script Gallery.

    Learn PowerShell  

    Script requests


    \_(ツ)_/

    Thursday, February 15, 2018 10:37 AM
  • Thanks for the rapid response! Since I am not aware with scripting I have decided to divide all the workstations into three groups, based on the output from the sccm query. After that I will create three separate main scripts. Unfortunately I do not have enough time to perform detailed scripting language learning. Anyway, thanks to anyone who read/commented this post.
    Thursday, February 15, 2018 11:35 AM
  • This should do what you want:

    # Set the new size of C:
    $NewSize = '120Gb'
    
    # Check if D is not a local disk:
    if (-not(Get-WmiObject win32_volume | Where-Object {$_.FileSystem -eq 'NTFS' -and $_.DriveLetter -eq 'D:'})){
    
        # Replace CD Drive Driveletter D with E, if present:
        Get-WmiObject win32_volume | Where-Object {$_.FileSystem -eq 'CDFS' -and $_.DriveLetter -eq 'D:'} | ForEach-Object {$_.DriveLetter = 'E:'; $_.Put()} 
    
        # Resize C to something new. Note that the value for the -Size parameter should be the NEW size of the partition.
        Get-Partition | Where-Object {$_.DriveLetter -eq 'D'} | ForEach-Object {
            Resize-Partition -DiskNumber $_.DiskNumber -PartitionNumber $_.PartitionNumber -Size $NewSize 
            New-Partition -DiskNumber $_.DiskNumber -DriveLetter D -UseMaximumSize
            Format-Volume -DriveLetter D -FileSystem NTFS
        }
    }


    Friday, February 16, 2018 2:17 PM