Need to create multiple mount folders using Add-PartitionAccessPath from a variable value

Hi gurus,

I need some help in being able to create multiple folders that will be used as mount points for newly formatted disks in a Windows system. The area I need help with is where I am using the -AccessPath commadlet. I am trying to use a $diskid variable to store my disk numbers and using it as part of the script to create the mounted folders and associate these to their corresponding disks. How can I specify which folder each disk should be mounted to using the -AccessPath commadlet?

##Get disk ID'setting
$diskid = Get-Disk | Where-Object PartitionStyle -Eq "RAW" | Select Number

##Provide the disk number
$Partition = "2"

foreach ($diskid in $diskids){
	Set-Disk -Number $diskid -IsReadOnly $false
	Get-Disk –Number $diskid | Initialize-Disk –PartitionStyle GPT
	New-Partition –DiskNumber $diskid –UseMaximumSize
	Add-PartitionAccessPath -DiskNumber $diskid -PartitionNumber $Partition –AccessPath "Z:\TEST1"
	Get-Partition –Disknumber $diskid –PartitionNumber $Partition | Format-Volume –FileSystem NTFS -AllocationUnitSize 65536 –UseLargeFRS –Confirm:$false

In the above script I am only specifying one folder but I would like to use up to 4. I.e., Z:TEST1, Z:TEST2, Z:TEST3, Z:TEST4. Thanks in advance!!

Little hazy on what you’re asking for. If you want the AccessPath to be dynamic based on the $Partition, that would be string concantenation:

$Partition = "2"
("Z:\TEST{0}" -f $Partition)

to produce:


Hi Rob,

Trying to use $diskid array to select the disk numbers and create a set. I then want to use this set to create named directories that correspond to each disk. Once the directories are created, I then want to use a foreach loop to perform all the disk commands for each disk in the set.

Using something similar to this statement I want to create 3 directories;

For ($i=1; $i -le 3; $i++) {New-Item C:\MOUNT\TEST$i –ItemType Directory} - Not sure how i cause the names.

Once the directories are created, using the disk numbers included in the set, I want to use the foreach statement to accomplish the following things;
1. Initialize the all the disks in the set
2. Set the partition style to GPT for all disks in the set
3. Create a new partition for all disks in the set
4. Add or mount the new partition for all the disks in the set to their corresponding folders created earlier
5. Get the partition of each disk in the set and format the volume using all the swiches

Perhaps the code below can do a better job explaining my idea. The code works and does what I needed to do but as you can see is inefficient and too long.

##Folder creation
New-Item C:\MOUNT\TEST –ItemType Directory
New-Item C:\MOUNT\TESTER –ItemType Directory
New-Item C:\MOUNT\TESTING –ItemType Directory

##Partition ID is 2
$Partition = 2

##Provision 3 disks using 1, 2 and 3 as the disk numbers
Set-Disk -Number 1 -IsReadOnly $false
Set-Disk -Number 2 -IsReadOnly $false
Set-Disk -Number 3 -IsReadOnly $false
Get-Disk –Number 1 | Initialize-Disk –PartitionStyle GPT
Get-Disk –Number 2 | Initialize-Disk –PartitionStyle GPT
Get-Disk –Number 3 | Initialize-Disk –PartitionStyle GPT
New-Partition –DiskNumber 1 –UseMaximumSize
New-Partition –DiskNumber 2 –UseMaximumSize
New-Partition –DiskNumber 3 –UseMaximumSize
Add-PartitionAccessPath -DiskNumber 1 -PartitionNumber $Partition –AccessPath “C:\MOUNT\TEST”
Add-PartitionAccessPath -DiskNumber 2 -PartitionNumber $Partition –AccessPath “C:\MOUNT\TESTER”
Add-PartitionAccessPath -DiskNumber 3 -PartitionNumber $Partition –AccessPath “C:\MOUNT\TESTING”
Get-Partition –Disknumber 1 –PartitionNumber $Partition | Format-Volume –FileSystem NTFS -AllocationUnitSize 65536 –UseLargeFRS –Confirm:$false
Get-Partition –Disknumber 2 –PartitionNumber $Partition | Format-Volume –FileSystem NTFS -AllocationUnitSize 65536 –UseLargeFRS –Confirm:$false
Get-Partition –Disknumber 3 –PartitionNumber $Partition | Format-Volume –FileSystem NTFS -AllocationUnitSize 65536 –UseLargeFRS –Confirm:$false

Use a lookup hashtable to associate a number with a value

$lookupHash = @{
    1 = 'TEST'
    2 = 'TESTER'
    3 = 'TESTING'

$Partition = 2

for ($i=1; $i -le 3; $i++) {
   #New-Item $lookupHash[$i] –ItemType Directory
   #Set-Disk -Number $i -IsReadOnly $false
   #Get-Disk –Number $i | Initialize-Disk –PartitionStyle GPT
   #New-Partition –DiskNumber $i –UseMaximumSize
   #Add-PartitionAccessPath -DiskNumber $i -PartitionNumber $Partition –AccessPath $lookupHash[$i]
   #Get-Partition –Disknumber $i –PartitionNumber $Partition | 
   #     Format-Volume –FileSystem NTFS -AllocationUnitSize 65536 –UseLargeFRS –Confirm:$false


1 Like

Hey Rob!

Thanks for your help and patience on this one. I ran the updated code and got the error below. Seems it isn’t liking the $i hash. Anything we need to add after it?

Add-PartitionAccessPath : Invalid Parameter
At line:15 char:4

  • Add-PartitionAccessPath -DiskNumber $i -PartitionNumber $Partition
    –AccessPat …
    + CategoryInfo          : InvalidArgument: (StorageWMI:ROOT/Microsoft/.../ 
   MSFT_Partition) [Add-PartitionAccessPath], CimException
    + FullyQualifiedErrorId : StorageWMI 5,Add-PartitionAccessPath

Well, this code assumes there are 3 disks, 1,2 and 3 and all are using Partition 2:

$lookupHash = @{
    1 = 'TEST'
    2 = 'TESTER'
    3 = 'TESTING'

$Partition = 2

for ($i=1; $i -le 3; $i++) {
   'Processing disk {0} with folder {1} on partition {2}' -f $i, $lookupHash[$i], $Partition


Processing disk 1 with local path TEST on partition 2   
Processing disk 2 with local path TESTER on partition 2 
Processing disk 3 with local path TESTING on partition 2

If there is no disk 2 or partition 2 or the folder path doesn’t exist, then you are going to get an error.