Table of Contents
Drive with the Name ‘”C’ Does not Exist
When starting write a PowerShell script. We want to prompt to enter a user list (.csv) using the Read-Host cmdlet. We add the file path by right clicking the file and choose copy as path and pasting into the below command. Looks like this: “C:\users.csv”.
This makes life easier as we can drag, drop or copy/paste rather than full typing the path.
PS C:\> $users = Read-Host "What is the file path of the csv file"
What is the file path of the csv file: "C:\users.csv"
PS C:\> Import-Csv -Path $users
Then we import using Import-Csv cmdlet. However, when we do that, it is saying below error:
Import-Csv : Cannot find drive. A drive with the name ‘”C’ does not exist.
At line:1 char:1
+ Import-Csv -Path $users
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (“C:String) [Import-Csv], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.ImportCsvCommand
The problem is that PowerShell keeps throwing errors, saying it can’t find the drive “X:. 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:\> $users
"C:\users.csv"
If we add the path without quotes, it will work just fine. Here is how it is captured:
PS C:\> $users = Read-Host "What is the file path of the csv file"
What is the file path of the csv file: C:\users.csv
PS C:\> Import-Csv -Path $users
OldUPN NewUPN
------ ------
[email protected] [email protected]
[email protected] [email protected]
[email protected] [email protected]
[email protected] [email protected]
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 Import-Csv sees it without quotes. See below example .
$users = Read-Host "What is the file path of the csv file"
$users = $users.trim('"')
Import-Csv -Path $users.trim('"')
PS C:\> $users = Read-Host "What is the file path of the csv file"
What is the file path of the csv file: "C:\users.csv"
PS C:\> $users = $users.trim('"')
PS C:\> $users
C:\users.csv
PS C:\> Import-Csv -Path $users
OldUPN NewUPN
------ ------
[email protected] [email protected]
[email protected] [email protected]
[email protected] [email protected]
[email protected] [email protected]
# Shorten your script
# Import-Csv -Path $users.trim('"')
Alternatively, you can use the below method to remove the double quotes from the user’s input strings.
$users = Read-Host "What is the file path of the csv file"
$users = $users.Replace('"', '')
Import-Csv -Path $users
PS C:\> $users = Read-Host "What is the file path of the csv file"
What is the file path of the csv file: "C:\users.csv"
PS C:\> $users = $users.Replace('"', '')
PS C:\> Import-Csv -Path $users
OldUPN NewUPN
------ ------
[email protected] [email protected]
[email protected] [email protected]
[email protected] [email protected]
[email protected] [email protected]
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: