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.

Thomas
  • 4,155
  • 5
  • 21
  • 28
SuperJMN
  • 151
  • 2
  • 9
  • 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

2 Answers2

4

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.

a2k42
  • 103
  • 2
Adam Parsons
  • 156
  • 3
  • 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 08: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 08: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 08:40
  • Try `Initialize-Disk -Number 0 -PartitionStyle GPT` and make sure the disk is not Read-only – Adam Parsons May 28 '18 at 08:52
  • The disk is already inialized. I cannot Initialize-Disk again since I would lose every partition and I don't want that :) – SuperJMN May 28 '18 at 09:28
  • `Get-Disk` and verify it is GPT..? – Adam Parsons May 28 '18 at 09:40
  • Change size to "512MB", and perhaps the information here incl. the comments may help - https://blogs.technet.microsoft.com/heyscriptingguy/2013/05/29/use-powershell-to-initialize-raw-disks-and-to-partition-and-format-volumes/ – Adam Parsons May 28 '18 at 10:08
  • Also @GregAskew mentioned if it doesn't work you may be able to create it as another partition type, then use `Set-Partition` - https://docs.microsoft.com/en-us/powershell/module/storage/set-partition?view=win10-ps – Adam Parsons May 28 '18 at 10:30
  • I've used "Set-Partion" to change the GptType, but it always tell me "the specified partition type is invalid". – SuperJMN May 31 '18 at 10:03
  • Set-Partition -PartitionNumber 43 -DiskNumber 4 -GptType c12a7328-f81f-11d2-ba4b-00a0c93ec93b Set-Partition : The specified partition type is not valid. – SuperJMN May 31 '18 at 10:03
  • OK, I've finally got it! you have to indicate the guid like this: Set-Partition -PartitionNumber 43 -DiskNumber 4 -GptType "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}" – SuperJMN May 31 '18 at 11:06
  • 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
JosefZ
  • 1,514
  • 1
  • 10
  • 18