Private/New-OSDPartitionWindows.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<#
.SYNOPSIS
New-OSDDisk Private Function
 
.DESCRIPTION
New-OSDDisk Private Function
 
.NOTES
19.10.10 Created by David Segura @SeguraOSD
#>

function New-OSDPartitionWindows {
    [CmdletBinding()]
    param (
        #Fixed Disk Number
        #For multiple Fixed Disks, use the SelectDisk parameter
        #Default = 0
        #Alias = Disk, Number
        [Alias('Disk','Number')]
        [int]$DiskNumber = 0,
        
        #Drive Label of the Windows Partition
        #Default = OS
        #Alias = LW, LabelW
        [Alias('LW','LabelW')]
        [string]$LabelWindows = 'OS',
        
        #Drive Label of the Recovery Partition
        #Default = Recovery
        #Alias = LR, LabelR
        [Alias('LR','LabelR')]
        [string]$LabelRecovery = 'Recovery',

        #Skips the creation of the Recovery Partition
        [switch]$SkipRecoveryPartition,

        #Size of the Recovery Partition
        #Default = 990MB
        #Range = 350MB - 80000MB (80GB)
        #Alias = SR, Recovery
        [Alias('SR','Recovery')]
        [ValidateRange(350MB,80000MB)]
        [uint64]$SizeRecovery = 990MB
    )

    #======================================================================================================
    #    GPT WINDOWS
    #======================================================================================================
    if ((Get-OSDGather -Property IsUEFI) -and ($SkipRecoveryPartition.IsPresent)) {
        $OSDDisk = Get-Disk -Number $DiskNumber
        if ($global:OSDDiskSandbox -eq $true) {
            $SizeWindows = $($OSDDisk.Size) - $SizeSystemGpt - $SizeMSR
            $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1)

            Write-Host "SANDBOX: New-Partition -DiskNumber $DiskNumber -UseMaximumSize -GptType {ebd0a0a2-b9e5-4433-87c0-68b6b72699c7} DriveLetter W" -ForegroundColor DarkGray
            Write-Host "SANDBOX: Format-Volume -DriveLetter W -FileSystem NTFS -NewFileSystemLabel $LabelWindows" -ForegroundColor DarkGray



        }
        if ($global:OSDDiskSandbox -eq $false) {
            Write-Warning "New-Partition -DiskNumber $DiskNumber -UseMaximumSize -GptType {ebd0a0a2-b9e5-4433-87c0-68b6b72699c7} DriveLetter W"
            $PartitionWindows = New-Partition -DiskNumber $DiskNumber -UseMaximumSize -GptType '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' -DriveLetter W

            Write-Warning "Format-Volume -DriveLetter W -FileSystem NTFS -NewFileSystemLabel $LabelWindows"
            #$null = Format-Volume -Partition $PartitionWindows -NewFileSystemLabel "$LabelWindows" -FileSystem NTFS -Force -Confirm:$false

$null = @"
select disk $DiskNumber
select partition $($PartitionWindows.PartitionNumber)
format fs=ntfs quick label="$LabelWindows"
exit
"@
 | diskpart.exe

        }
    }

    #======================================================================================================
    #    GPT WINDOWS + RECOVERY
    #======================================================================================================
    if ((Get-OSDGather -Property IsUEFI) -and (! $SkipRecoveryPartition.IsPresent)) {
        $OSDDisk = Get-Disk -Number $DiskNumber
        if ($global:OSDDiskSandbox -eq $true) {
            $SizeWindows = $($OSDDisk.Size) - $SizeSystemGpt - $SizeMSR - $SizeRecovery
            $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1)

            Write-Host "SANDBOX: New-Partition GptType {ebd0a0a2-b9e5-4433-87c0-68b6b72699c7} Size $($SizeWindowsGB)GB DriveLetter W" -ForegroundColor DarkGray
            Write-Host "SANDBOX: Format-Volume -DriveLetter W -FileSystem NTFS -NewFileSystemLabel $LabelWindows" -ForegroundColor DarkGray
            Write-Host "SANDBOX: New-Partition GptType {de94bba4-06d1-4d40-a16a-bfd50179d6ac} UseMaximumSize" -ForegroundColor DarkGray
            Write-Host "SANDBOX: Format-Volume FileSystem NTFS NewFileSystemLabel $LabelRecovery" -ForegroundColor DarkGray
            Write-Host "SANDBOX: Set-Partition Attributes 0x8000000000000001" -ForegroundColor DarkGray
        }
        if ($global:OSDDiskSandbox -eq $false) {
            $SizeWindows = $($OSDDisk.LargestFreeExtent) - $SizeRecovery
            $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1)

            Write-Warning "New-Partition GptType {ebd0a0a2-b9e5-4433-87c0-68b6b72699c7} Size $($SizeWindowsGB)GB DriveLetter W"
            $PartitionWindows = New-Partition -DiskNumber $DiskNumber -Size $SizeWindows -GptType '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' -DriveLetter W

            Write-Warning "Format-Volume -DriveLetter W -FileSystem NTFS -NewFileSystemLabel $LabelWindows"
            #$null = Format-Volume -Partition $PartitionWindows -NewFileSystemLabel "$LabelWindows" -FileSystem NTFS -Force -Confirm:$false

$null = @"
select disk $DiskNumber
select partition $($PartitionWindows.PartitionNumber)
format fs=ntfs quick label="$LabelWindows"
exit
"@
 | diskpart.exe

            Write-Warning "New-Partition GptType {de94bba4-06d1-4d40-a16a-bfd50179d6ac} UseMaximumSize"
            $PartitionRecovery = New-Partition -DiskNumber $DiskNumber -GptType '{de94bba4-06d1-4d40-a16a-bfd50179d6ac}' -UseMaximumSize

            Write-Warning "Format-Volume FileSystem NTFS NewFileSystemLabel $LabelRecovery"
            #$null = Format-Volume -Partition $PartitionRecovery -NewFileSystemLabel "$LabelRecovery" -FileSystem NTFS -Confirm:$false

$null = @"
select disk $DiskNumber
select partition $($PartitionRecovery.PartitionNumber)
format fs=ntfs quick label="$LabelRecovery"
exit
"@
 | diskpart.exe

            Write-Warning "Set-Partition Attributes 0x8000000000000001"
$null = @"
select disk $DiskNumber
select partition $($PartitionRecovery.PartitionNumber)
gpt attributes=0x8000000000000001
exit
"@
 | diskpart.exe
        }
    }

    #======================================================================================================
    #    MBR WINDOWS
    #======================================================================================================
    if ((! (Get-OSDGather -Property IsUEFI)) -and ($SkipRecoveryPartition.IsPresent)) {
        if ($global:OSDDiskSandbox -eq $true) {
            $SizeWindows = $($OSDDisk.Size) - $SizeSystemMbr
            $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1)
            Write-Host "SANDBOX: New-Partition -DiskNumber $DiskNumber -UseMaximumSize -MbrType IFS -DriveLetter W" -ForegroundColor DarkGray
            Write-Host "SANDBOX: Format-Volume -DriveLetter W -FileSystem NTFS -NewFileSystemLabel $LabelWindows" -ForegroundColor DarkGray
        }
        if ($global:OSDDiskSandbox -eq $false) {
            Write-Warning "New-Partition -DiskNumber $DiskNumber -UseMaximumSize -MbrType IFS -DriveLetter W"
            $PartitionWindows = New-Partition -DiskNumber $DiskNumber -UseMaximumSize -MbrType IFS -DriveLetter W
    
            Write-Warning "Format-Volume -DriveLetter W -FileSystem NTFS -NewFileSystemLabel $LabelWindows"
            #$null = Format-Volume -Partition $PartitionWindows -NewFileSystemLabel "$LabelWindows" -FileSystem NTFS -Force -Confirm:$false
$null = @"
select disk $DiskNumber
select partition $($PartitionWindows.PartitionNumber)
format fs=ntfs quick label="$LabelWindows"
exit
"@
 | diskpart.exe
        }
    }

    #======================================================================================================
    #    MBR WINDOWS + RECOVERY
    #======================================================================================================
    if ((! (Get-OSDGather -Property IsUEFI)) -and (! $SkipRecoveryPartition.IsPresent)) {

        if ($global:OSDDiskSandbox -eq $true) {
            $SizeWindows = $($OSDDisk.Size) - $SizeSystemMbr - $SizeRecovery
            $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1)

            Write-Host "SANDBOX: New-Partition -DiskNumber $DiskNumber -Size $SizeWindows -MbrType IFS -DriveLetter W" -ForegroundColor DarkGray
            Write-Host "SANDBOX: Format-Volume FileSystem NTFS NewFileSystemLabel $LabelWindows" -ForegroundColor DarkGray
            Write-Host "SANDBOX: New-Partition -DiskNumber $DiskNumber -UseMaximumSize" -ForegroundColor DarkGray
            Write-Host "SANDBOX: Format-Volume -FileSystem NTFS -NewFileSystemLabel $LabelRecovery" -ForegroundColor DarkGray
            Write-Host "SANDBOX: Set-Partition id 27" -ForegroundColor DarkGray
        }
        if ($global:OSDDiskSandbox -eq $false) {
            $OSDDisk = Get-Disk -Number $DiskNumber
            $SizeWindows = $($OSDDisk.LargestFreeExtent) - $SizeRecovery
            $SizeWindowsGB = [math]::Round($SizeWindows / 1GB,1)
    
            Write-Warning "New-Partition -DiskNumber $DiskNumber -Size $SizeWindows -MbrType IFS -DriveLetter W"
            $PartitionWindows = New-Partition -DiskNumber $DiskNumber -Size $SizeWindows -MbrType IFS -DriveLetter W
    
            Write-Warning "Format-Volume FileSystem NTFS NewFileSystemLabel $LabelWindows"
            #$null = Format-Volume -Partition $PartitionWindows -NewFileSystemLabel "$LabelWindows" -FileSystem NTFS -Force -Confirm:$false
$null = @"
select disk $DiskNumber
select partition $($PartitionWindows.PartitionNumber)
format fs=ntfs quick label="$LabelWindows"
exit
"@
 | diskpart.exe

            Write-Warning "New-Partition -DiskNumber $DiskNumber -UseMaximumSize"
            $PartitionRecovery = New-Partition -DiskNumber $DiskNumber -UseMaximumSize
    
            Write-Warning "Format-Volume -FileSystem NTFS -NewFileSystemLabel $LabelRecovery"
            #$null = Format-Volume -Partition $PartitionRecovery -NewFileSystemLabel "$LabelRecovery" -FileSystem NTFS -Confirm:$false
$null = @"
select disk $DiskNumber
select partition $($PartitionRecovery.PartitionNumber)
format fs=ntfs quick label="$LabelRecovery"
exit
"@
 | diskpart.exe

            Write-Warning "Set-Partition id 27"
$null = @"
select disk $DiskNumber
select partition $($PartitionRecovery.PartitionNumber)
set id=27
exit
"@
 | diskpart.exe
        }
    }
}