Search code examples
powershellformatramdisk

Format a RAM disk with no drive letter by using `Format-Volume` cmdlet


Background

I have a RAM disk created with ImDisk Toolkit. The drive letter is "R". I can access the RAM disk properly (Get-ChildItem R: shows directory entries properly).

I want to format the RAM disk in my powershell script (it runs a benchmark) without administrator authority. So I want to refrain from using format command because it needs administrator authority to execute.

Problem

When I tried to format the RAM disk with Format-Volume PowerShell cmdlet, I got the following error:

PS C:\> Format-Volume -DriveLetter R
Format-Volume : No MSFT_Volume objects found with property 'DriveLetter' equal to 'R'.  Verify the value of the
property and retry.
At line:1 char:1
+ Format-Volume -DriveLetter R
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (R:Char) [Format-Volume], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound_DriveLetter,Format-Volume

And I found the RAM disk seems to have no drive letter from PowerShell (with Get-Volume).

DriveLetter FriendlyName     FileSystemType DriveType HealthStatus OperationalStatus SizeRemaining     Size
----------- ------------     -------------- --------- ------------ ----------------- -------------     ----
E                            Unknown        Fixed     Healthy      Unknown                     0 B      0 B
C           Windows          NTFS           Fixed     Healthy      OK                    334.99 GB 475.7 GB
            Windows RE tools NTFS           Fixed     Healthy      OK                    504.46 MB   990 MB

I have tried to choose the RAM disk with FriendlyName property, but I couldn't access that property. FriendlyName seems to be not actual property (it is not listed with Get-Member). So I couldn't filter the result of Get-Volume and pass it to Format-Volume.

Question

How can I specify the RAM disk to format with Format-Volume cmdlet that seems to have no drive letter with Get-Volume? Or, do I have to use format command rather than Format-Volume cmdlet (so I must have administrator authority) in this situation ?

Edit

I found the RAM disk doesn't appear in the result of Get-Volume, Get-CimInstance Win32_Volume or Get-CimInstance Win32_DiskPartition. It appears only in the result of Get-CimInstance Win32_LogicalDisk like this:

PS C:\> Get-CimInstance Win32_LogicalDisk

DeviceID DriveType ProviderName VolumeName Size          FreeSpace
-------- --------- ------------ ---------- ----          ---------
C:       3                      Windows    510781288448  353026121728
E:       3
R:       3                                 1073737728    1056030720
Z:       3                                 3897664995328 3646232199168

Note that the RAM disk (R:) is shown as DriveType 3 (Fixed disk).

I could get the object of the RAM disk (R:), but I got following error with Format-Volume.

PS C:\> $ramDisk = Get-CimInstance Win32_LogicalDisk | Where-Object { $_.DeviceID -eq "R:" }
PS C:\> Format-Volume -Partition $ramDisk -WhatIf
Format-Volume : Cannot bind argument to parameter 'Partition', because PSTypeNames of the argument do not match the
PSTypeName required by the parameter: Microsoft.Management.Infrastructure.CimInstance#MSFT_Partition.
At line:1 char:26
+ Format-Volume -Partition $ramDisk -WhatIf
+                          ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Format-Volume], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : MismatchedPSTypeName,Format-Volume

Edit 2

Format-Volume -InputObject also returns following error.

PS C:\> $ramDisk = Get-CimInstance Win32_LogicalDisk | Where DeviceId -eq 'R:'
PS C:\> $ramDisk

DeviceID DriveType ProviderName VolumeName Size       FreeSpace
-------- --------- ------------ ---------- ----       ---------
R:       3                                 1073737728 1056030720

PS C:\> Format-Volume -InputObject $ramDisk -WhatIf
Format-Volume : Cannot bind argument to parameter 'InputObject', because PSTypeNames of the argument do not match the P
STypeName required by the parameter: Microsoft.Management.Infrastructure.CimInstance#MSFT_Volume.
At line:1 char:28
+ Format-Volume -InputObject $ramDisk -WhatIf
+                            ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Format-Volume], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : MismatchedPSTypeName,Format-Volume


Solution

  • I confirmed at users forum that ImDisk has no interface for Volume Mount Manager. So there is no way to format the RAM disk made with ImDisk with Format-Volume cmdlet.

    Note: I had also tried to format a RAM disk made with Dataram RAMDisk. It has an interface for Volume Mount Manager, but Format-Volume -DriveLetter S eventually requires the administrator authority.

    PS C:\> Format-Volume -DriveLetter S
    Format-Volume : Access Denied
    Activity ID: {1c815d1b-72f2-4432-b0e1-a33c96d2f539}
    At line:1 char:1
    + Format-Volume -DriveLetter S
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (StorageWMI:ROOT/Microsoft/...age/MSFT_Volume) [Format-Volume], CimExc
       eption
        + FullyQualifiedErrorId : StorageWMI 40001,Format-Volume
    

    It seems the Format-Volume cmdlet requires the administrator authority anyway.