Problem with mapping a network drive in PS

Hram Admin 290 Reputation points
2025-05-21T10:34:21.0933333+00:00

Hello!

Would you please try helping me with the strange problem: I can't map a network drive in the script with the correct network credentials.

I have many scripts that use the following string (for example):

net use H: \10.10.10.10\someFOLDER /user:"domain\user1" "nnnfff444"

...and never had any issues with it: while I'm supplying to the script the correct credentials, the network drive H: gets connected flawlessly.

~Last week I started seeing the strange behaivour: either brand new scripts that I'm currently working on or the old ones that have already been running for months started showing the following (in the case of old scripts this behaviour can be seen only when I run this particular string of the script manually! - when the script is running on the server no problems arise).

  1. I try to map a disk in PS using the right user name and password:
    01

2 The disk does not map and the following event gets registered on the server containing the share:
02

3 I then try to open the share in the explorer: I copy/paste the username and password from the script to the Enter your credentials dialog and can successfully access the share:
04

4 The event 4624 (success) with the same credentials:
05

Q1: What does it mean???

And one more problem that I see only in the cases described above: I always use error checking when running some commands and in case the error arises I use something like

Error.Clear
COMMAND
Write-Output "..... the error was $Error[0].Exception.Message"

-but in these cases the $Error[0] is always empty, the first error is in the $Error[1]:
03

Q2: Why?

Regards,
Michael Firsov

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. MotoX80 37,161 Reputation points
    2025-05-21T13:54:50.56+00:00

    Do you have any special characters, like dollar signs, in the password? Powershell may be trying to interpret them instead of just passing them on to net.exe.

    Try putting single quotes around the password.

    net use H: \\test10\c$ /user:"admin" 'admin'
    net use H: /del 
    

    User's image

    Your image shows that you launched Powershell_ISE with "run as administrator". You may be running into a conflict between elevated and non-elevated processes. Explorer does not run elevated. If the script normally runs unelevated, then test that way, don't use "run as administrator".

    https://learn.microsoft.com/en-us/troubleshoot/windows-client/networking/mapped-drives-not-available-from-elevated-command

    Net.exe doesn't know anything about Powershell error variables. You need to check $LASTEXITCODE.

    $error.clear()
    net use H: \\test10\c$ /user:"admin" "aaaaaaaaaaaaa"
    "Net.exe RC=$LASTEXITCODE"
    $error
     
    $error.clear()
    net use H: \\test10\c$ /user:"admin" "admin"
    "Net.exe RC=$LASTEXITCODE"
    $error
    

    User's image

    If you want to reference $error, then use the Powershell cmdlets instead of net.exe.

    $User = ".\admin"
    $PWord = ConvertTo-SecureString -String "admin" -AsPlainText -Force
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    New-PSDrive H -PSProvider FileSystem -Root "\\test10\c$" -Credential $cred  -persist      
    remove-psdrive H
    $error.clear() 
    $User = ".\admin"
    $PWord = ConvertTo-SecureString -String "aaaaaaaaaaaaa" -AsPlainText -Force
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    New-PSDrive H -PSProvider FileSystem -Root "\\test10\c$" -Credential $cred  -persist      
    $error[0]
    $error[1]
    
    
    

    User's image

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Hram Admin 290 Reputation points
    2025-05-22T07:30:40.2733333+00:00

    MotoX80, yes, that's it!!!

    "net use H: \test10\c$ /user:"admin" 'admin' - single quotes do really work - thanks a lot (yes, there was $ in the password)!!!

    Regarding the $Error:

    1. thank you for the net.exe's replacement
    2. if Net.exe doesn't know anything about Powershell error variables then why the $Error[1] (instead of $Error[0]) does return the correct error???

  2. Hram Admin 290 Reputation points
    2025-05-23T07:45:17.2433333+00:00

    MotoX80, thank you very much for your help!

    Regards,
    Michael

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.