Powershell 命令列安裝 Windows 作業系統

奕王千歲發表於2021-09-20

使用 powershell 完全安裝或重灌 windows 作業系統的正確姿勢

note:完全使用 powershell 指令,絕非在 powershell 終端下鍵入傳統的 cmd 指令。使用傳統的 dism.exe 指令可以展開 windows 映像,但是最大的問題是沒有一個快捷的掛載 iso 映象的方式,為此到了 powershell,完全是另一番新的天地。完全可以使用 mount-diskimage 即可輕鬆掛載 iso 映象

 

1.初始化磁碟

initialize-disk   -number   磁碟編號    

2.建立分割槽 note:

      Specifies the type of GPT partition to create (by GUID). By default, the New-Partition cmdlet creates a basic G

        PT data partition.

 

        The GUIDs of valid types are:

 

         -- System Partition (c12a7328-f81f-11d2-ba4b-00a0c93ec93b)

 

         -- Microsoft Reserved (e3c9e316-0b5c-4db8-817d-f92df00215ae)

 

         -- Basic data (ebd0a0a2-b9e5-4433-87c0-68b6b72699c7)

 

         -- Microsoft Recovery (de94bba4-06d1-4d40-a16a-bfd50179d6ac)

 

 

new-partition   -disknumber   磁碟編號   -size   分割槽大小    -assigndriveletter   | format-volume   -filesystem  "檔案系統"    -newfilesystemlabel  "卷標名稱"

 

比如使用第一塊磁碟 建立 esp 分割槽:300MB   

new-partition   -disknumber   1   -size   300MB    -assigndriveletter   -gpttype 

"{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}"  | format-volume   -filesystem  "FAT32"    -newfilesystemlabel  "ESP"

 

比如使用第一塊磁碟 建立 MSR 分割槽:200MB  無需碟符

new-partition   -disknumber   1   -size   200MB    -gpttype "{e3c9e316-0b5c4db8-817d-f92df00215ae}"  | format-volume   -filesystem  "NTFS"    newfilesystemlabel  "MSR"

 

比如使用第一塊磁碟 建立 Windows 分割槽:剩餘空間

new-partition   -disknumber   1   -usemaximumsize     -assigndriveletter  | formatvolume   -filesystem  "NTFS"    -newfilesystemlabel  "OS"

 

如果是需要建立 mbr 的系統主分割槽(啟用分割槽),請使用:

new-partition   -disknumber   1   -usemaximumsize     -assigndriveletter -mbrtype  -ifs   -isactive  | format-volume   -filesystem  "NTFS"    -newfilesystemlabel  "OS" 注:

 

    -size 指定分割槽大小,單位 MB 或 GB. 

    -usemaximumsize 如果想把所有剩餘空間都作為一個分割槽請使用-usemaximumsize 選項即可

    -assigndriveletter:表示自動為分割槽分配碟符。如果是 MSR(微軟保留分割槽)則不需要此選項。因為保留分割槽不需要碟符,不是屬於普通使用者使用,一般也用不上

    |:表示管道。類unix使用者(linux,unix)應該熟悉管道。當然除了powershell,還有傳統的cmd也支援管道

    -filesystem:表示檔案系統:支援 fat32,exfat,ntfs,慎用 refs 檔案系統

   -newfilesystemlabel:為分割槽命名卷標

   如果你的磁碟是 mbr 需要轉換為 gpt,請使用 mbr2gpt 命令。

   mbr2gpt  /validate    /disk:磁碟編號    /allowfullos

 

  

掛載映象:假設你的 D:\有一個 windows10.iso 的系統映象

mount-diskimage   -imagepath   D:\windows10.iso

 

get-volume:檢視卷資訊

 

假設剛剛掛載的系統映象,碟符是 I:\

 

展開映像:

    比如:將 I:\sources\install.wim 映像解壓到 G:\

    expand-windowsimage  -imagepath  I:\sources\install.wim  -index  版本索引號  applypath   G:\

 

 索引號可以根據  get-windowsimage 指令獲得。比如

 get-windowsimage -imagepath F:\sources\install.wim

 

以下選擇可選。新增 系統引導:

bcdboot  C:\Windows   -s  C:\    -f  BIOS 新增 BIOS 引導:

bcdboot  C:\Windows   -s  Z:\    -f  UEFI 新增 UEFI 引導:

bcdboot  C:\Windows   -s  Z:\    -f  ALL 新增BIOS+UEFI引導

注:C:\表示 windows 驅動器;Z:\表示引導驅動器

 

restart-computer:重啟系統  進入系統初始化,完成系統安裝 

 完整過程圖示如下:

 

 

轉載請註明出處!謝謝!

相關文章