I'm trying to write a script which when ran, will detect an external drive I've added and then Initialize, Parition and then format it. The safety mechanism will be the 'FriendlyName' of the disk, which is the same for all disks I use. I usually bulk do this, so need to script it to save time.
I've come up with the following four lines:
1: Clear-Disk -FriendlyName 'ST3000DM 2CS' -RemoveData -Confirm:$false
2: Initialize-Disk -FriendlyName 'ST3000DM 2CS' -PartitionStyle MBR
3: New-Partition -DiskNumber 1 -UseMaximumSize -AssignDriveLetter
4: Format-Volume -DriveLetter F -Confirm:$false
The "FriendlyName" can be seen when you run 'Get-Disk'
Cutting and pasting these is fine, the job gets done.
Two problems:
1) Lines 3 and 4 refer to a hardcoded DiskNumber and DriveLetter. DiskNumber is assigned after execution of line 2. DriveLetter is then assigned on execution of line 3. I want lines 3 and 4 to be more failsafe and want line 3 to execute New-Partition specifically on the outcome of line 2 (i.e. dependant on disk number assigned), and line 4 to execute Format-Volume based on the outcome of line 3 (i.e. dependant on drive letter assigned).
2) I want to stick all of this into a ps1 script for easy double click to run action.
Can you help?