New-PSDrive : The specified drive root either does not exist, or it is not a folder

Hi, I have some files that I have to copy daily in a network drive for backup. The network drive is already mapped and I can access it with the credentials on Windows Explorer, but the Powershell script is giving me errors.

First thing to point out is that if I log in via Windows Explorer first Copy-Item works fine, the problem is when is Powershell who has to manage the login.

Here is my code so far:

$Path1 = "C:\Origine\giornaliero.csv"
$Path2 = "\\192.168.91.249\Verbali ragioneria"
$password = "my_psw"
$user = "my_user"

$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force

$cred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $user, $securePassword

New-PSDrive -Name "Y" -PSProvider FileSystem -Root "\\192.168.91.249\Verbali ragioneria" -Credential $credentials

Copy-Item $Path1 -Destination $Path2

The error I’m getting is the following:

New-PSDrive : The specified drive root “\192.168.91.249\Verbali ragioneria” either does not exist, or it is not a folder.

If I try Get-ChildItem on the same folder it says: *

Get-ChildItem : Cannot find path ‘\192.168.91.249\Verbali ragioneria’ because it does not exist.

However if I login first via Windows Explorer get-ChilItem works fine.

Any idea? Ty in advance

Instead of tinkering around with New-PsDrive and credentials I’d grant the needed rights to the account running the script and avoid all that clutter. That’ll make your life much much easier.

BTW: Do you really store the password in your code in plain text? :thinking:

1 Like

The drive I need to access requires a username and password that cannot be stored (I don’t care much if they are in plain sight, the code is in a secure server anyway and the destination folder is on a NAS in the office).

Is really New-PsDrive so difficult to use?

I’ve got this. And why don’t you use the account you need to access the drive to run the script? :man_shrugging:

I have never had to use it. But I have read quite often that it caused issues. :man_shrugging:

Have you tried using net use instead of New-PsDrive?

It’s still a bad idea anyway. :man_shrugging: … and it does not seem to work … :man_shrugging: :man_shrugging:

I see something that could be the issue. Your share on the server has a space in the name. This could be confusing the commands. From my admin days, I always avoided spaces in share names specifically because they made my life more difficult. Might not be the issue, but it raises a red flag for me.

Also, I would strongly encourage you to not get into the habit of storing plain text passwords in code. It’s too easy to slip up and put important credentials somewhere insecure.

Recommended reading: Microsoft.PowerShell.SecretManagement Module - PowerShell | Microsoft Learn

This can be set up without a password - so it’s secured by your Windows authentication, which is far more secure than plain text and still usable by automation.

I agree with the other comments, there are many better alternatives then storing the password in plain text. One thing to consider is any powershell code ran on a system is logged in eventviewer meaning your plain text password would show in there as well. So you may have the script and server locked down but the risk for that password being exposed through other means goes up exponentially by storing the password in plain text. If you must use username/password, look into the secret management that Darwin suggested or look at Export-Clixml.

As far as the issue goes, I don’t have any problems with New-PsDrive and based on the error it appears the path only has one leading slash

\192.168.91.249\Verbali ragioneria

vs

\\192.168.91.249\Verbali ragioneria

1 Like

That could be caused by the formatting of the forum software. @Darkveemon1 used “Quote” instead of “preformatted text” (code) … usually that swallows one of the backslashs. As you can see in the code formatted correctly there are 2 leading backslashs.

Then my next suspicion is maybe OP has improper quotes around the path. If any code was copy/pasted this can occur easily, as I know you’re aware of Olaf. OP, try retyping all your quotes manually in your code and see if you get the same error. If you’re sure the path exists, then there has to be some explanation for the error.

Hi Darkveemon1,

You are passing $credentials in for the -Credential parameter. Change that typo to $cred and try again. :grinning_face:

swooped

2 Likes

Ok, now I feel so dumb, thank you it worked

The details, they are so easy to overlook. Great catch!

When you encounter errors in freshly written code, it’s a good idea to insert Set-StrictMode -Version Latest at the beginning of the code.
Earlier versions of PowerShell used Set-PSDebug -Strict.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.