Search code examples
powershellwindows-server-2012windows-server-2012-r2disk-partitioning

Assign a mount point folder path to a drive using PowerShell


I have a server that is provisioned with several disks. One for an OS, one for programs etc., and the remaining are to be mounted to empty NTFS folders.

Disk 0: C:
Disk 1: G:
Disk 2: G:\Folder01\
Disk 3: G:\Folder02\

This is fairly straightforward through the GUI: https://technet.microsoft.com/en-us/library/cc753321.aspx

But, I need to automate so I need to figure out how to do this with PowerShell. I have hit several dead ends:

I thought I was making some progress with Get-Disk, Initialize-Disk, and New-Partition

$Disk = Get-Disk 2
$Disk | Initialize-Disk -PartitionStyle MBR
$Disk | New-Partition -UseMaximumSize -MbrType IFS
$Partition = Get-Partition -DiskNumber $Disk.Number

From here, I was hoping to do something like:

New-Volume $Partition -FriendlyName Folder01 -AccessPath F:\Folder01 -FileSystem NTFS

But that doesn't produce any output and I noticed that earlier when I created a partition, it created a volume as well.

I'm thinking that New-Volume might be the wrong command because the help indicates that the input object should be a MSFT_StoragePool object.

I tried to go down that path a little ways with commands like: Get-PhysicalDisk, New-StoragePool, Get-StoragePool

$Disk = Get-PhysicalDisk -FriendlyName PhysicalDisk2
New-StoragePool -FriendlyName Pool2 -StorageSubsystemFriendlyName "Storage Spaces*" -PhysicalDisks $Disk
$Pool = Get-StoragePool Pool2
New-Volume -FriendlyName Folder01 -AccessPath G:\Folder01 -FileSystem NTFS -InputObject $Pool

But I just get an error message at this point for invalid parameters, which I think is weird because I was able to use Get-Member to confirm that $Pool is a MSFT_StoragePool.

I've also noticed that after creating that storage pool, my disk disappeared from Disk Management and no longer shows up with Get-Disk

I think that shows that Storage Pools are something completely different from what I want to actually do.

How do I assign a drive to an NTFS Folder with Powershell?


Solution

  • The Commandlet that I needed was Add-PartitionAccessPath

    I set things up the way I wanted through the GUI on disk 2, created a partion on Disk 3 and then carefully compared the properties. The key was here:

    (Get-Partition -DiskNumber 2).AccessPaths
    (Get-Partition -DiskNumber 3).AccessPaths
    

    There was an additional AccessPath for Disk2, which led me across a number of MSDN articles looking for a method, until I finally found that there already is a command for it.

    The following command sealed the deal:

    $Partition | Add-PartitionAccessPath -AccessPath "G:\Folder01"
    

    For reference, here's the full solution:

    $Disk = Get-Disk 3
    # $Disk | Clear-Disk -RemoveData -Confirm:$false
    $Disk | Initialize-Disk -PartitionStyle MBR
    $disk | New-Partition -UseMaximumSize -MbrType IFS
    $Partition = Get-Partition -DiskNumber $Disk.Number
    $Partition | Format-Volume -FileSystem NTFS -Confirm:$false
    New-Item -ItemType Directory -Path "G:\Folder01"
    $Partition | Add-PartitionAccessPath -AccessPath "G:\Folder01"