2

I would like to automate a script that creates an EFI partition.

Currently, one step is manual and requires to start DISKPART and execute these commands:

create partition efi size=100 
format fs=fat32 quick label=SYSTEM 
assign letter=S

How can I get the same result using PowerShell? I haven't seen any option in the New-Partition cmdlet to create an EFI partition.

  • 1
    You should be able to use -GptType '{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}'. If that doesn't work, you can create it as another partition type and use Set-Partition with GptType. – Greg Askew May 26 '18 at 12:58
3

I suspect this would be the equivalent in PowerShell:

New-Partition -DiskNumber 0 -Size 100MB -GptType "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}" -DriveLetter "S"
Format-Volume -FileSystem FAT32 -NewFileSystemLabel "SYSTEM" -DriveLetter "S" -Force

Modify as appropriate to suit your use-case.

  • Can you, please, verify it works? I'm getting "Invalid GPT type" errors in for the New-Partition command. Thank you! – SuperJMN May 28 '18 at 8:24
  • Sorry, the GPT type is taken directly from the MSDN docs, you may need to reformat the disk first and make sure it's selecting the right disk? – Adam Parsons May 28 '18 at 8:34
  • I did exactly that. Unfortunately, it refuses to work: Invalid GPT Type. However, the The GUID is OK according to the docs. – SuperJMN May 28 '18 at 8:40
  • Try Initialize-Disk -Number 0 -PartitionStyle GPT and make sure the disk is not Read-only – Adam Parsons May 28 '18 at 8:52
  • 1
    Hahaha oh gosh, I'm sorry. Thanks for updating my answer – Adam Parsons May 31 '18 at 17:17
1

Please note that you need to select a disk before you can create a partition.

Read diskpart /?, use :

Microsoft DiskPart syntax:
        diskpart [/s <script>] [/?]

        /s <script> - Use a DiskPart script.
        /?          - Show this help screen.

Note that a DiskPart script is simple plain text file where the diskpart commands are placed in (one command per line), for instance MyDiskpart.txt listed below.

diskpart /s MyDiskpart.txt

Answer: The latter command you can run from an elevated powershell session as well as from an elevated cmd session (˙diskpart˙ always requires elevation).

Addendum:

Moreover, Diskpart accepts input from a pipe ('|') as well as from < redirection so that the following cmd commands are equivalent:

diskpart /s MyDiskpart.txt
diskpart<MyDiskpart.txt
type MyDiskpart.txt|diskpart

The latter commands tested using the following diskpart script:

==> type MyDiskpart.txt
list disk
list volume
select disk 1
list partition

Moreover, you need not to create a diskpart script file; the following .bat script displays system volume details (selecting volume # dynamically):

@ECHO OFF
SETLOCAL EnableExtensions

:check_permissions
echo Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
    echo Success: Administrative permissions confirmed.
) else (
    echo Failure: Current permissions inadequate.
    goto :endlocal
)

:do_the_job
for /f "tokens=2" %%a in ('echo list volume ^| diskpart ^| findstr System') do (
    (
        echo select volume %%a
        echo detail volume
    ) | diskpart
)
echo DONE
:endlocal
pause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.