New-PSDrive Mapping Not Working With Hidden Network Share

This topic contains 6 replies, has 4 voices, and was last updated by  Don Jones 1 year, 10 months ago.

  • Author
  • #53623


    I have a script that is replacing old server names with new ones for mapped drives. It is working fine for most drives. However, it is not mapping the drive for hidden drives. You may ask why I'm using "net use" to delete the mappings. That is because I was receiving errors when piping results from Get-PSDrive and then creating the new drive again – using net use to delete the drive is the only way I could find to get rid of all traces. That is not why I am here though. I am having a hard time mapping hidden shares (shares with $ at the end). An example is below. The error I receive is "New-PSDrive : The network resource type is not correct." It seems like the only difference between this one (one that is not working) and one that is working is the dollar sign. Any ideas how I can get this to work?

    $OldServer3 = "\\OLDSERVER\SHARE$"
    $NewServer3 = "\\NEWSERVER\SHARE$"
    $Drives3 = Get-WmiObject win32_mappedlogicaldisk | ? {$_.ProviderName -like "$($OldServer3)*" } 
    If ($Drives3) {
        Foreach ($drive in $Drives3) { 
                "Replacing '$OldServer3' with '$NewServer3' for drive $($drive.DeviceID)" | Out-File $Log -Force -Append
                Net use /delete /yes $drive.DeviceID
                $Name = (($drive.DeviceID) -replace ":", "")
                $NewRoot = (($drive.ProviderName) -replace [regex]::Escape($OldServer3), $NewServer3)
                New-PSDrive -Name $Name -PSProvider FileSystem -Root $NewRoot -Persist -Scope global -Confirm:$false -ea SilentlyContinue
  • #53627

    Daniel Krebs


    You'll need to escape the dollar-sign with a backtick. Check out the "Escaping a variable" section in the about_Escape_Characters help:

    $OldServer3 = "\\OLDSERVER\SHARE`$"

    I hope that helps.


    • This reply was modified 1 year, 10 months ago by  Daniel Krebs.
    • #53717



      This does not appear to be working. The $NewRoot variable shows the dollar sign, not sure if that is the issue. I am using a back-tick before the dollar sign on both the old server and new server path. However, the same error happens.

  • #53712

    Edmond Yee

    If there are no variables in $oldserver3 or $newserver3, you can also enclose the path in single quotes, which will take the literal text inside of the quotes, even if there is a reserved character like a $ inside them.

  • #53721


    Even if changing the path directly for the "New-PSDrive" part, it doesn't work.

    Does not work (New-PSDrive : The network resource type is not correct):

    New-PSDrive -Name $Name -PSProvider FileSystem -Root "\\NEWSERVER\SHARE$" -Persist -Scope global -Confirm:$false

    Does not work (New-PSDrive : The network resource type is not correct):

    New-PSDrive -Name $Name -PSProvider FileSystem -Root "\\NEWSERVER\SHARE`$" -Persist -Scope global -Confirm:$false
    • This reply was modified 1 year, 10 months ago by  Jeff.
  • #53728


    Turns out it was the new server – The new server share is not hidden, but the old one was. Sorry for the confusion! Dollar sign works just fine in the variable.

  • #53732

    Don Jones

    That's odd. When you use -Persist, PowerShell actually calls an underlying Win32 API, and just passes all the information to it. Your first example should definitely not work, unless you switch to single-quotes, because of the $. I'm wondering if the string is just being passed literally in the second example, and the backtick is causing a problem. Could you try this using single quotes around the UNC? And also verify that this works using NET USE?

You must be logged in to reply to this topic.