Powershell cmdlets one-by-one or How to replace diskpart with powershell

Yes, yes I know… The Swedish Powershell MVP Niklas Goude will probably kick my ass if he ever found out I wrote this blog post…
 
But not all of us were born composing powershell scripts with our eyes closed, so here are some baby steps (one-by-one cmdlets) to help you get started with understanding how awesome powershell is 🙂
 
What I did was that I sat down and got-help in powershell and actually figured out how to create a virtual machine and a virtual disk a.k.a I’m trying to step away from diskpart (that we all know and love)
 
Creating a virtual machine, mounting an ISO and starting it, FTW
 
First of all, you need Powershell 3.0 to accomplish this, that either means you need a Windows Server 2012 or Windows 8 host with Hyper-V installed. (Yes Hyper-V is included in both the client and the server)
 
Create, start a virtual machine and mount an ISO with Powershell
 
Run Powershell as administrator and execute the following:
New-VM -Name VM01 -MemoryStartupBytes 1024MB
The command creates a new virtual machine named VM01 with 1024 MB memory.
 
Now you need to create a virtual disk for the machine:
 
New-VHD -Path C:\VM\VM01.vhdx -SizeBytes 30GB -Dynamic
After creating the disk you’d want to attach it to the virtual machine
Add-VMHardDiskDrive -VMName VM01 -Path C:\VM\VM01.vhdx
Then to mount an ISO you execute
Set-VMDvdDrive -VMName VM01 -ControllerNumber 1 -Path C:\ISO\windows.ISO
And then start the machine!
 
Start-vm –name VM01
 
 
Create, select, partition and format VHD in Powershell or…
“How to replace diskpart with powershell”
 
So diskpart, create vdisk and so on, how-to do the same thing in powershell…
 
Create a virtual disk
 
New-VHD -Path C:\VM\windows.vhdx -SizeBytes 30GB -Dynamic
 
Mount the VHD (instead of attaching it in diskpart)
 
Mount-VHD -Path C:\VM\windows.vhdx
 
Find out which disk number the mounted VHD got
 
Get-Disk
 
Bring the disk online (and YES you specify 1 instead of 0 if you want to take it offline)
 
Set-Disk -Number 2 -IsOffline 0
 
Make the disk writable (and YES you specify it to be Read only with 1)
 
Set-Disk -Number 2 -isReadOnly 0
 
Initialize the disk
 
Initialize-Disk -Number 2 -PartitionStyle MBR
 
Create a partition on disk 2 and automatically assign a driveletter
 
New-Partition -Disknumber 2 -UseMaximumSize -AssignDriveLetter
 
Format the volume
 
Get-Partition -Disknumber 2 | Format-Volume -FileSystem FAT32
By the way, if you try to eject the virtual disk in windows explorer or disk management you will most likely get an error message, so if you want to

Dismount the VHD execute the following
Dismount-VHD -Path C:\vm\windows.vhdx

And then there are the rest of the Storage Cmdelts awesomeness

Advertisements