Assign a mount point folder path to a drive using PowerShell
Asked Answered
S

4

9

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?

Sclar answered 24/8, 2015 at 22:4 Comment(0)
S
16

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"
Sclar answered 24/8, 2015 at 23:49 Comment(0)
P
3

How to assign a drive to a NTFS Folder in Powershell using Command Line:

cmd /c 'subst G: "C:\Folder1"'

Note: Explorer is showing the same size of the folder as the C:.

Pulchia answered 24/8, 2015 at 22:17 Comment(3)
I don't think this is going to accomplish what I want, but I'm going to give it a try and see if the result is identical to what I get when I do it via GUI. I initialized a disk and assigned it to J:, I then tried the command: subst J: G:\Folder01 and got Invalid parameter - J: I'm running sfc /scannow to see if I can resolve that issue.Sclar
Ok, I figured out why this isn't working for me. Subst allows me to take a drive letter that's currently unassigned and use it as a shorctut to another path. That's cool, but not what I'm actually trying to do here. I want to take a "drive" by which I mean a physical or virtual disk, and mount it to a folder, so that when I put files into that folder, they're stored on that disk.Sclar
Thanks, this was what I was looking for. I use it to show dumped xray (from CD) to my physician. I guess it works in some situations, where application need to be in the root folder of a drive.Lempres
D
1

If you're looking at putting files into some folder, and expect them to end up being stored on some disk then there's MOUNTVOL.

To find drive/volume name:

MOUNTVOL

TO remove the drive letter & instead mount on an empty NTFS folder:

mountvol e: /d
mountvol c:\<path> <volume name> 
eg. mountvol c:\fakedisk\ \\?\Volume{########-####-####-####-############}\

So in above example, everytime to write to c:\fakedisk\ folder, the data will end up in what used to be e: drive (disk/device). This is opposite to subst, which "Associates a path with a drive letter ", so writing data to the drive will get stored in that path/folder.

Tested on Win 10 CMD

Daradarach answered 5/2, 2021 at 15:18 Comment(4)
This might be useful on the fly, but a key part of the problem I was trying to solve was automating/scripting the task. To link this command to the rest of the script, you'd need to provide the guid. I think you can get that with $Partition.guid within the original example, but I haven't tested it. I think Add-PartitionAccessPath is more straightforward, but having multiple ways of solving a problem is always helpful.Sclar
To find drive/volume name (GUID): type MOUNTVOL. It will list "values for VolumeName along with current mount points". Then select GUID associated with the mount point to your drive or folder path.Daradarach
Sure, but again, the task here is to automate the process. PowerShell was listed as a requirement in the question, although non-PowerShell commands certainly can be incorporated if necessary. If one were to use MOUNTVOL, then the next step would be to parse the output, which honestly seems like a challenging task. If I'm going to do everything manually, I might as well just use the GUI.Sclar
You mean to automatically select the GUID for the selected drive?Daradarach
A
0

Make sure the folder exists before running this command:

$Partition | Add-PartitionAccessPath -AccessPath "G:\Folder01"
Amylaceous answered 29/5, 2018 at 19:32 Comment(1)
You probably should add that as a comment to my solution above instead of as a separate answer. I've modified my solution to include a step to create the folder.Sclar

© 2022 - 2024 — McMap. All rights reserved.