Table of Contents
The Filename, Directory Name, or Volume Label Syntax is Incorrect
In this post, we’ve a simple PowerShell script to require users to enter the full path of an ISO image, then mount it using Mount-DiskImage cmdlet.
$imagePath = Read-Host "Enter the ISO path"
Mount-DiskImage -ImagePath $imagePath
User inserts the file path by right clicking the iso file and select copy as path and pasting into the prompt. Looks like this: “D:\images\Win11.iso”. Then we got the below error.
Enter the ISO path: “D:\images\Win11.iso”
Mount-DiskImage : The filename, directory name, or volume label syntax is incorrect.
At D:\quotes.ps1:2 char:1
+ Mount-DiskImage -ImagePath $imagePath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_DiskImage:ROOT/Microsoft/…/MSFT_DiskImage) [Mount-DiskImage], CimE
xception
+ FullyQualifiedErrorId : HRESULT 0x8007007b,Mount-DiskImage
This seems to be being caused by the quotes around the path. PowerShell treating the quote as part of the path rather than just a quote.
PS C:\> $imagePath
"D:\images\Win11.iso"
If we add the path without quotes, it will work just fine. Here is how it is captured:
PS C:\> $imagePath = Read-Host "Enter the ISO path"
Enter the ISO path: D:\images\Win11.iso
PS C:\> Mount-DiskImage -ImagePath $imagePath
Attached : True
BlockSize : 0
DevicePath : \\.\CDROM0
FileSize : 5557432320
ImagePath : D:\images\Win11.iso
LogicalSectorSize : 2048
Number : 0
Size : 5557432320
StorageType : 1
PSComputerName :
Read-Host treats the variable type a string, so you are adding the quotes as part of your entry to Read-Host will lead the issue. To fix it, you can replace the quotations in your path so that Mount-DiskImage sees it without quotes. See below example.
$imagePath = Read-Host "Enter the ISO path"
$imagePath = $imagePath.trim('"')
Mount-DiskImage -ImagePath $imagePath
As you can see, this time even we’ve entered the file path with the double quotes. The script works without any error and the image file has been mounted as a virtual disk.
PS C:\> $imagePath = Read-Host "Enter the ISO path"
Enter the ISO path: "D:\images\Win11.iso"
PS C:\> $imagePath = $imagePath.trim('"')
PS C:\> Mount-DiskImage -ImagePath $imagePath
Attached : True
BlockSize : 0
DevicePath : \\.\CDROM0
FileSize : 5557432320
ImagePath : D:\images\Win11.iso
LogicalSectorSize : 2048
Number : 0
Size : 5557432320
StorageType : 1
PSComputerName :
Alternatively, you can use the below method to remove the double quotes from the user’s input strings. This method use Replace instead of Trim to remove the double quote.
$imagePath = Read-Host "Enter the ISO path"
$imagePath = $imagePath.Replace('"', '')
Mount-DiskImage -ImagePath $imagePath
Conclusion
When running a PowerShell script requires user input:
- Educate users enter input without the double quotes.
- Or you can edit your codes to remove the doube quotes, then you don’t need to care about user imput with or without double quotes.
Not a reader? Watch this related video tutorial: