Locating Mount Points Using PowerShell

Sometimes when you are building a file server (or some other server), you come to realize that you have more drive or partitions than drive letters that are available. In this situation, what do you do? If you answered ‘create a mount point’ to handle this issue, then you are on the right path, or at least taking that path that I am going on.

Mount points can be defined as:

Volume Mount Points are specialized NTFS filesystem objects which are used to mount and provide an entry point to other volumes. Mount points can be created in a directory on an NTFS file system, which gives a reference to the root directory of the mounted volume.

Source

If you want to know how to create a mount point, check out this link here.

I have created 2 mount points, as you can see in the pictures below.

image image

image

In PowerShell, we can typically use Win32_LogicalDisk to find out information about each drive and its space. But guess what? This will not work so well with mount points.

Get-WmiObject Win32_LogicalDisk

 

image

Hmm, so if we cannot use the Win32_LogicalDisk class to locate these elusive mount points and find out their size, what could we use instead to figure this out? The answer lies with the Win32_Volume class instead. Whereas the Win32_LogicalDisk “represents a data source that resolves to an actual local storage device on a computer system running Windows”, the Win32_Volume class “represents an area of storage on a hard disk”. This means that even mount points will be available to us to view all sorts of information on. Because there will be a lot of data returned if I just query the class, I am only going to return what I need initially, which is the Name, Caption, FreeSpace  and Capacity.

Get-WmiObject Win32_Volume | Format-Table Name, Label, FreeSpace, Capacity

 

image

Perfect! You can see the mount points listed here in the output along with their free space and total size. Now I will take this a step further by cleaning up some of the output by removing the DVD-Drive (E:\) and converting the space from bytes to GB. I can filter by DriveType for only 3, which means I am only looking for a local disk and I can convert the bytes to GB simply by taking each value and dividing by 1GB.

Get-WmiObject Win32_Volume -Filter "DriveType='3'" | ForEach {
    New-Object PSObject -Property @{
        Name = $_.Name
        Label = $_.Label
        FreeSpace_GB = ([Math]::Round($_.FreeSpace /1GB,2))
        TotalSize_GB = ([Math]::Round($_.Capacity /1GB,2))
    }
}

image

So there you have it! Finding a mount point is not only easy to do, but also provides you with the amount of space as well! There are many other properties that you may find useful such as the serial number, block size and file system.

Also, I felt it was worth pointing out that there are some great methods associated with the Win32Volume class that are very useful. They are:

Method

Description
AddMountPoint Adds a mount point directory for the volume.
Chkdsk Invokes the Chkdsk operation on the volume.
Defrag Defragments the volume.
DefragAnalysis Generates a fragmentation analysis for the volume.
Dismount Dismounts a volume from the file system.
ExcludeFromAutoChk Excludes volumes from the Chkdsk operation to be run at the next reboot.
Format Formats the volume.
Mount Mounts a volume to the file system.
ScheduleAutoChk Schedules Chkdsk to be run at the next reboot if the dirty bit of the volume is set.

Feel free to give them a checkout and see how cool they are!

This entry was posted in powershell and tagged , , . Bookmark the permalink.

13 Responses to Locating Mount Points Using PowerShell

  1. zaphodikus says:

    Um, how to remove an added mountpoint? Add-mountpoint gives me what I want, but I cannot remove the mountpoint later?
    Any clues as to why I’m missing something obvious here in this API
    Calling dismount($true, $true) pretty much leaves me with a borked mountpoint, the filesystem knows there is a mountpoint, but it’s inaccessible after the WMI dismount call.

    • Ben Conrad says:

      zaphodikus, did you ever get dismount($true, $true) working? Doesn’t actually do anything. The returnvalue = 2 (Volume Has Mount Points) which is correct. I’m running on Windows 2008 so I don’t have access to the newer Windows 2012 cmdlets…

  2. Pingback: Change mount points drive letter | Powershell

  3. Pingback: Creating Pop-ups by Using PowerShell - Hey, Scripting Guy! Blog - Site Home - TechNet Blogs

  4. Wesal says:

    I am trying to create a mount point if a clustered disk using powershell. Any way to do it.

  5. AK says:

    Do you know why Win32_LogicalDisk does not return anything for BlockSize?

  6. Straightforward and simplified. Thank you!

  7. is there any chance to search files e.g. *.log in Mount Points? Unfortunately there are tons of tips how to check free space but nothing to search drives with included mount points. :-7

  8. Kumar says:

    Please share me for multiple servers at a time like providing servers list and out put has to come in csv format. I ahve tried below one but not working properly. please check

    $attention= 0.11;
    $crtical=0.05;
    $servers = Get-Content “D:\Powershell_Help\spacechecks\servers.txt”;
    $file = New-Item -type file -Force “D:\Powershell_Help\spacechecks\output.txt”
    foreach($server in $servers)
    {
    Get-WmiObject Win32_Volume -Filter “DriveType=’3′” | ForEach {
    New-Object PSObject -Property @{
    Name = $_.Name
    Label = $_.Label
    FreeSpace_GB = ([Math]::Round($_.FreeSpace /1GB,2))
    TotalSize_GB = ([Math]::Round($_.Capacity /1GB,2))
    }
    }
    }

    • Yogeesh V. Kamath says:

      You are reading the file for remote servers and you have them running in loop; but not referring them in Get-WmiObject command. So that never connects to remote server. Below worked for me. Added “-ComputerName $server”

      BTW: I found some issues with quotes around “DriveType=’3′” and “D:\Powershell_Help\spacechecks\servers.txt” when I copied the code from your post. Hope, it’s HTML issue.

      $servers = Get-Content “C:\Apps\Win_Servers.txt”;
      $file = New-Item -type file -Force “C:\Apps\Win_Server_output.txt”
      foreach($server in $servers)
      {
      Get-WmiObject Win32_Volume -ComputerName $server -Filter “DriveType=’3′” | ForEach {
      New-Object PSObject -Property @{
      Name = $_.Name
      Label = $_.Label
      FreeSpace_GB = ([Math]::Round($_.FreeSpace /1GB,2))
      TotalSize_GB = ([Math]::Round($_.Capacity /1GB,2))
      }
      }
      }

  9. brahma says:

    nice article. do you help on how to load these into sql server?

    Thanks in advance
    brahma

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s