locked
How to create a mount point with PowerShell RRS feed

  • Question

  • Hello,

    Can you please point me to a sample which creates a mount point using powershell?

    Monday, March 14, 2011 2:24 PM

Answers

All replies

  • http://technet.microsoft.com/en-us/library/cc753321.aspx

    $script = "select volume 0`r`nassign mount=`"C:\Test`""
    $script | diskpart
    

    • Proposed as answer by Kazun Monday, March 14, 2011 3:08 PM
    • Unproposed as answer by Kazun Tuesday, March 15, 2011 7:25 AM
    Monday, March 14, 2011 2:36 PM
  • Is there nothing native in powershell to create the mount point? want to get rid of launching EXEs from my code.
    Monday, March 14, 2011 2:45 PM
  • Is there nothing native in powershell to create the mount point? want to get rid of launching EXEs from my code.
    Diskpart is present on all systems starting with Windows 2000 and above.I think it will be very difficult to implement similar functionality in. NET.
    Monday, March 14, 2011 2:52 PM
  • I think this is possible using Powershell alone

     

    Set-WMIInstance -Class Win32_MountPoint

    but I don't know the rest of the syntax.

    Monday, March 14, 2011 3:20 PM
  • Win32_MountPoint Class does not have methods or writable properties.
    Monday, March 14, 2011 3:25 PM
  • Hi,

     

    Win32_MountPoint class only gives the directory and volume ID information. If you want to more information, you’d better link to the Win32_Volume class. Win32_Volume class has the method AddMountPoint.

     

    Win32_Volume

    http://msdn.microsoft.com/en-us/library/aa394515%28v=vs.85%29.aspx

     

    BTW, I found the following article that could be helpful:

     

    What do I create a Mount point from the command line?

    http://www.windowsitpro.com/article/file-systems/what-do-i-create-a-mount-point-from-the-command-line-.aspx

     

    Best Regards

    Dale


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    • Marked as answer by MSDN Student Wednesday, March 16, 2011 6:32 AM
    Tuesday, March 15, 2011 7:15 AM
  • One way to perform the task of adding and removing mount points is to utilise the kernel32.dll methods 'DeleteVolumeMountPoint' and 'SetVolumeMountPoint'. Here is an example which mounts a volume to C:\Test. It relies on checking Win32_Volume and Win32_MountPoint to check current Mount Points and Volume RELPaths.

    Add-Type @"
      using System;
      using System.Runtime.InteropServices;
      public class MountPoint
      {
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern bool DeleteVolumeMountPoint(string mountPoint);
        [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern bool SetVolumeMountPoint(string mountPoint,string volumeName);
      }
    "@
    $destpath = "C:\Test"
    $volume = gwmi Win32_Volume | ?{ $_.Label = "Test" }
    $mount = gwmi -Class Win32_MountPoint | ?{ $vol.__RELPATH -eq $_.Volume }
    $dir = (([String]$mount.Directory).Split("=")[1]).Replace( "\\" , "\" )
    #Remove the Volume Mount Point if it is not targiting our destination
    $dir = $dir.Replace('"',"")
    if (($dir -ne "") -and ("$dir" -ne "$($destpath)"))
    {
      $ret = [MountPoint]::DeleteVolumeMountPoint($dir)
      $dir = ""
    }
    if  (($dir -eq "") -or ($dir -eq $null))
    {
      #Create Folder if it is not yet there
      if (!(Test-Path $destpath))
      {
        New-Item $destpath -type directory
      }
      $ret = [MountPoint]::SetVolumeMountPoint($destpath,$volume.DeviceId)
    }

    Thanks, Chris.

    • Edited by Dwarfsoft Tuesday, January 22, 2013 2:29 AM Missed the Add-Type.
    Tuesday, January 22, 2013 2:27 AM
  • Quick update for Windows 8.1/Server 2012 R2:

    Add-PartitionAccessPath (details: http://technet.microsoft.com/en-us/library/hh848703.aspx) allows you to create a volume mount point.

    Try this: get some info on your drives using

    Get-Partition


    Now use something like:

    
    
    Add-PartitionAccessPath -DiskNumber 5 -PartitionNumber 2 -AccessPath 'c:\ExchangeDatabases'
    



    Regards, Paul www.servercare.nl




    Thursday, September 25, 2014 9:24 AM
  • Try this. I cooked it up by consolidating a few different sources.  I posted it on the microsoft connect suggestion for native support, under workarounds: https://connect.microsoft.com/PowerShell/feedback/details/627099/need-powershell-equivalent-to-cmds-mklink

    #symlinks.ps1
    
    # First add a type so it stays available for this instance
    Add-Type -TypeDefinition @'
    using System;
    using System.Runtime.InteropServices;
    
    namespace mklink
    {
        public class symlink
        {
            [DllImport("kernel32.dll")]
            public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags);
            [DllImport("kernel32.dll")]
            public static extern bool RemoveDirectory(string lpPathName);
            [DllImport("kernel32.dll")]
            public static extern uint GetLastError();
        }
    }
    '@
    
    Function New-ReparsePoint() {
         <# 
        .SYNOPSIS 
            Creates a reparse point to the specified target.
             
        .DESCRIPTION 
            Creates a reparse point to the specified target.
            
        .PARAMETER Path
            Path to the reparse point to remove. 
             
        .NOTES 
            Author: Jordan Mills
            Version: 1.0
         
        .EXAMPLE 
            Remove-SymbolicLink -Path "E:\directory\mount"
        #>    
        [cmdletbinding(DefaultParameterSetName="default")]
        Param (
            [parameter(
                ParameterSetName="default",
                Position=0,
                Mandatory=$true,
                ValueFromPipeLine=$True,
                ValueFromPipelineByPropertyName=$True
            )]
            [parameter(
                ParameterSetName="file",
                Position=0,
                Mandatory=$true,
                ValueFromPipeLine=$True,
                ValueFromPipelineByPropertyName=$True
            )]
            [parameter(
                ParameterSetName="directory",
                Position=0,
                Mandatory=$true,
                ValueFromPipeLine=$True,
                ValueFromPipelineByPropertyName=$True
            )]
            [Alias("Path","FileName","Directory")]
            [string[]]$Name,
            [parameter(
                ParameterSetName="default",
                Position=1,
                Mandatory=$true,
                ValueFromPipelineByPropertyName=$True
            )]
            [parameter(
                ParameterSetName="file",
                Position=1,
                Mandatory=$true,
                ValueFromPipelineByPropertyName=$True
            )]
            [parameter(
                ParameterSetName="directory",
                Position=1,
                Mandatory=$true,
                ValueFromPipelineByPropertyName=$True
            )]
            [string]$TargetPath,
            [parameter(
                ParameterSetName="file",
                Position=2,
                Mandatory=$false,
                ValueFromPipelineByPropertyName=$True
            )] 
            [switch]$IsFile,
            [parameter(
                ParameterSetName="directory",
                Position=2,
                Mandatory=$false,
                ValueFromPipelineByPropertyName=$True
            )]
            [switch]$IsDirectory
        )
    
        If ($IsFile -or $IsDirectory) {
            If($IsFile) {
                $result = [mklink.symlink]::CreateSymbolicLink($Name,$TargetPath,0)
            } 
            Else {
                If($IsDirectory) {
                    $result = [mklink.symlink]::CreateSymbolicLink($Name,$TargetPath,1)
                } 
                Else {
                    Write-Error -Message "Conflicting path type parameters. This should not happen."
                    Break;
                }
            }
        } 
        Else {
            If (Test-Path -LiteralPath $TargetPath -PathType Leaf -ErrorAction SilentlyContinue) {
                $result = [mklink.symlink]::CreateSymbolicLink($Name,$TargetPath,0)
            } 
            Else {
                If (Test-Path -LiteralPath $TargetPath -PathType Container -ErrorAction SilentlyContinue) {
                    $result = [mklink.symlink]::CreateSymbolicLink($Name,$TargetPath,1)
                } 
                Else {
                    Write-Error -Message "Unable to determine path type of TargetPath. Use -IsFile or -IsDirectory."
                    Break;
                }
            }
        }
    
        If ($result) { 
            Get-Item $Name
        } Else {
            Write-Error -Message "Error creating symbolic link" -Category WriteError #-ErrorId $([mklink.symlink]::GetLastError())
        }
    }
    
    Function Remove-ReparsePoint {
         <# 
        .SYNOPSIS 
            Removes a file or directory that is a reparse point (symlink or hardlink) without removing all child objects.
             
        .DESCRIPTION 
            Removes a file or directory that is a reparse point (symlink or hardlink) without removing all child objects.
            
        .PARAMETER Path
            Path to the reparse point to remove. 
             
        .NOTES 
            Author: Jordan Mills
            Version: 1.0
         
        .EXAMPLE 
            Remove-SymbolicLink -Path "E:\directory\mount"
        #>    
        [cmdletbinding()]
        Param (
            [parameter(
                Position=0,
                Mandatory=$true,
                ValueFromPipeLine=$True,
                ValueFromPipelineByPropertyName=$True
            )]
            [Alias("FullName","Name","FileName","Directory")]
            [ValidateScript({Test-Path $_})] 
            [string[]]$Path
        )
        
        $Path |
        Get-Item |
        ForEach-Object {
            $Item = $_;
            Switch ($Item.Attributes -band ([IO.FileAttributes]::ReparsePoint -bor [IO.FileAttributes]::Directory)) {
                ([IO.FileAttributes]::ReparsePoint -bor [IO.FileAttributes]::Directory) {
                    # Is reparse directory / symlink
                    If ($whatif) {
                        Write-Host "What if: Performing the operation `"Delete Directory`" on target `"$($_.FullName)`""
                    } Else {
                        [System.IO.Directory]::Delete($Item.FullName);
                        Break;
                    }
                }
                ([IO.FileAttributes]::ReparsePoint -bor 0) {
                    # Is reparse file / hardlink
                    If ($whatif) {
                        Write-Host "What if: Performing the operation `"Delete File`" on target `"$($_.FullName)`""
                    } Else {
                        [System.IO.File]::Delete($Item.FullName);
                        Break;
                    }
                }
                default {
                    Write-Error "$Item is not a reparse point."
                }
            }
        }    
    }

    Thursday, September 25, 2014 6:37 PM
  • Quick update for Windows 8.1/Server 2012 R2:

    Add-PartitionAccessPath (details: http://technet.microsoft.com/en-us/library/hh848703.aspx) allows you to create a volume mount point.

    Try this: get some info on your drives using

    Get-Partition


    Now use something like:

    
    
    Add-PartitionAccessPath -DiskNumber 5 -PartitionNumber 2 -AccessPath 'c:\ExchangeDatabases'



    Regards, Paul www.servercare.nl




    Thanks, solved my issue!!
    Monday, May 22, 2017 2:36 PM