Небольшой скрипт, который обнуляет #HDD / #SSD и создаёт файловую систему заново.
-DN
-P_DiskNumber
- номер диска. Можно узнать командой Get-Disk
. Но если не ввести номер диска в параметры скрипта при запуске, скрипт сам выведет информацию и спросит номер диска. Обязательный параметр.-DL
-P_DriveLetter
- буква будущего тома. При создании тома, ОС должна присвоить ему букву. Обязательный параметр.-FS
-P_FileSystem
- файловая система. Может принимать следующие значения:'FAT'
- файловая система FAT.'FAT32'
- файловая система FAT32.'exFAT'
- файловая система exFAT.'NTFS'
- файловая система NTFS.'ReFS'
- новая файловая система ReFS.
-FSL
-P_FileSystemLabel
- метка файловой системы.-S
-P_Sleep
- перерыв между командами (в секундах).
Скрипт, запущенный без параметров, сам спросит необходимые данные:
Обнулить диск 3
, новому разделу присвоить букву E
, форматировать в NTFS
и указать метку USB-SSD
:
1
| .\pwsh.disk.erase.ps1 -DN 3 -DL 'E' -FS 'NTFS' -FSL 'USB-SSD'
|
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
| <#PSScriptInfo
.VERSION 0.1.0
.GUID 8d95f4d5-4d14-412c-9fa4-b75ebe79df09
.AUTHOR Kai Kimera
.AUTHOREMAIL mail@kai.kim
.COMPANYNAME Library Online
.COPYRIGHT 2023 Library Online. All rights reserved.
.LICENSEURI https://choosealicense.com/licenses/mit/
.PROJECTURI https://lib.onl/ru/2023/10/52d75b90-0637-5ba6-91d6-b1bff40e1d67/
#>
#Requires -Version 7.2
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Disk erase script.
.DESCRIPTION
Disk cleanup followed by partition creation.
.PARAMETER P_DiskNumber
Specifies the disk number of the disk on which to perform the clear operation. For a list of available disks, see the 'Get-Disk' cmdlet.
.PARAMETER P_FileSystem
Specifies the file system with which to format the volume.
The acceptable values for this parameter are: 'NTFS', 'ReFS', 'exFAT', 'FAT32' and 'FAT'.
.PARAMETER P_Sleep
Sleep time (in seconds).
.EXAMPLE
.\pwsh.disk.erase.ps1 -DN 3 -DL 'E' -FS 'NTFS' -FSL 'USB-SSD'
.LINK
https://lib.onl/ru/2023/10/52d75b90-0637-5ba6-91d6-b1bff40e1d67/
#>
# -------------------------------------------------------------------------------------------------------------------- #
# CONFIGURATION
# -------------------------------------------------------------------------------------------------------------------- #
param(
[Parameter(HelpMessage="Specify the disk number.")]
[ValidatePattern('^[0-9]+$')]
[Alias('DN')][int]$P_DiskNumber,
[Parameter(HelpMessage="Specify the file system to format the volume.")]
[ValidateSet('FAT', 'FAT32', 'exFAT', 'NTFS', 'ReFS')]
[Alias('FS')][string]$P_FileSystem,
[Parameter(HelpMessage="Sleep time (in seconds).")]
[Alias('S')][int]$P_Sleep = 2
)
# New line separator.
$NL = "$([Environment]::NewLine)"
# Random number.
$Random = "$(Get-Random -Minimum 1000 -Maximum 9999)"
# Getting free drive letter and to assign to the new partition.
$DriveLetter = "$((68..90 | ForEach-Object { $L=[char]$_; if ((Get-PSDrive).Name -notContains $L) { $L } })[0])"
# Specifying a new label to use for the volume.
$FileSystemLabel = "DISK_${Random}"
# -------------------------------------------------------------------------------------------------------------------- #
# INITIALIZATION
# -------------------------------------------------------------------------------------------------------------------- #
function Start-Script() {
Start-DPDiskList # Showing disk list.
if (-not $P_DiskNumber) { $P_DiskNumber = (Read-Host -Prompt 'Disk number') } # Getting disk number.
if (-not $P_FileSystem) { $P_FileSystem = (Read-Host -Prompt 'File system') } # Getting file system.
Start-DPDiskClear # Starting clearing disk.
Start-DPDiskInit # Initializing disk.
Start-DPDiskPartition # Creating partition.
Start-DPDiskFormat # Formatting volume.
}
# -------------------------------------------------------------------------------------------------------------------- #
# DISK LIST
# -------------------------------------------------------------------------------------------------------------------- #
function Start-DPDiskList() {
Write-Msg -T 'HL' -M 'Disk List'
Get-Disk
}
# -------------------------------------------------------------------------------------------------------------------- #
# CLEAR DISK
# -------------------------------------------------------------------------------------------------------------------- #
function Start-DPDiskClear() {
Write-Msg -T 'HL' -M "[DISK #${P_DiskNumber}] Clear Disk"
Write-Msg -T 'W' -A 'Inquire' -M ("You specified drive number '${P_DiskNumber}'.${NL}" +
"All data will be DELETED!")
$Param = @{
Number = $P_DiskNumber
RemoveData = $true
RemoveOEM = $true
Confirm = $false
}
Clear-Disk @Param
Start-Sleep -s $P_Sleep
}
# -------------------------------------------------------------------------------------------------------------------- #
# INITIALIZE DISK
# -------------------------------------------------------------------------------------------------------------------- #
function Start-DPDiskInit() {
Write-Msg -T 'HL' -M "[DISK #${P_DiskNumber}] Initialize Disk"
$Param = @{
Number = $P_DiskNumber
PartitionStyle = 'GPT'
}
Initialize-Disk @Param
Start-Sleep -s $P_Sleep
}
# -------------------------------------------------------------------------------------------------------------------- #
# CREATE PARTITION
# -------------------------------------------------------------------------------------------------------------------- #
function Start-DPDiskPartition() {
Write-Msg -T 'HL' -M "[DISK #${P_DiskNumber}] Create Partition"
$Param = @{
DiskNumber = $P_DiskNumber
DriveLetter = "${DriveLetter}"
UseMaximumSize = $true
}
New-Partition @Param
Start-Sleep -s $P_Sleep
}
# -------------------------------------------------------------------------------------------------------------------- #
# FORMAT DISK VOLUME
# -------------------------------------------------------------------------------------------------------------------- #
function Start-DPDiskFormat() {
Write-Msg -T 'HL' -M "[DISK #${P_DiskNumber}] Format Disk Volume (${DriveLetter} / ${P_FileSystem})"
if (-not $P_FileSystem) { $P_FileSystem = 'NTFS' }
$Param = @{
DriveLetter = "${DriveLetter}"
FileSystem = "${P_FileSystem}"
NewFileSystemLabel = "${FileSystemLabel}"
Force = $true
}
Format-Volume @Param
Start-Sleep -s $P_Sleep
}
# -------------------------------------------------------------------------------------------------------------------- #
# ------------------------------------------------< COMMON FUNCTIONS >------------------------------------------------ #
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
# SYSTEM MESSAGES
# -------------------------------------------------------------------------------------------------------------------- #
function Write-Msg() {
param (
[Alias('T')][string]$Type,
[Alias('M')][string]$Message,
[Alias('A')][string]$Action = 'Continue'
)
switch ($Type) {
'HL' { Write-Host "${NL}--- ${Message}".ToUpper() -ForegroundColor Blue }
'I' { Write-Information -MessageData "${Message}" -InformationAction "${Action}" }
'W' { Write-Warning -Message "${Message}" -WarningAction "${Action}" }
'E' { Write-Error -Message "${Message}" -ErrorAction "${Action}" }
default { Write-Host "${Message}" }
}
}
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------< RUNNING SCRIPT >------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
Start-Script
|