0

I want to change a mapped drive remote path. but I'm unable to filter the remote path property. Is there any way I can filter all the mapped drives remote path only so, later on, I can run a foreach loop to change the values? Thanks.

    Get-Item -Path HKCU:\Network | Where-Object -FilterScript {'RemotePath'}


    Hive: HKEY_CURRENT_USER\Network


Name                           Property                                                                                                                                                       
----                           --------                                                                                                                                                       
Z                              RemotePath     : \\IAPC\Users\IA\Documents\10                                                                                                                  
                               UserName       :                                                                                                                                               
                               ProviderName   : Microsoft Windows Network                                                                                                                     
                               ProviderType   : 131072                                                                                                                                        
                               ConnectionType : 1                                                                                                                                             
                               ConnectFlags   : 0                                                                                                                                             
                               DeferFlags     : 4                                                                                                                                             
                               UseOptions     : {68, 101, 102, 67...}                                                                                                                         



PS HKCU:\Network> 

Only interested in name of the network drive with RemotePath (under Property column)

2 Answers 2

1

The following outputs [pscustomobject] instances representing the values of those registry subkeys of HKCU:\Network whose RemotePath value is non-empty:

Get-ItemProperty -Path HKCU:\Network\* | Where-Object RemotePath

To get just the drive-name-remote-path pairs:

Get-ItemProperty -Path HKCU:\Network\* | Where-Object RemotePath | 
  Select-Object PSChildName, RemotePath

Note: The PSChildName property contains the drive letter of each mapping (it is the name of the subkey whose values are being returned).


To loop over all mappings of interest and update them by replacing the server-name component:

$oldServer = '\\IAPC\'
$newServer = '\\localhost\'
# CAVEAT: This instantly updates your drive mappings.
#         You can add -WhatIf to the Set-ItemProperty call,
#         to *preview* the operation, but it will only show the
#         target registry key and value, not the new data.
Get-ItemProperty -Path HKCU:\Network\* | Where-Object RemotePath | ForEach-Object {
  $driveLetter, $remotePath = $_.PSChildName, $_.RemotePath
  Set-ItemProperty -LiteralPath HKCU:\Network\$driveLetter RemotePath ($remotePath -replace [regex]::Escape($oldServer), $newServer)
}

As for what you tried:

  • Where-Object -FilterScript {'RemotePath'} is a no-op, because any input meets the criterion 'RemotePath', which, as a non-empty string literal is invariably $true when interpreted as a Boolean. To access a property on the current input object, you need to use automatic $_ variable: { $_.RemotePath }

    • It is only with the simplified syntax, shown above, which doesn't use a script block ({ ... }) that the string argument given is implicitly interpreted as the name of the property to access on the input object at hand.
  • Get-Item -Path HKCU:\Network only targets the root key of all network mappings itself, not the subkeys that define the actual mappings.

    • In your case you're also interested in the RemotePath value of each subkey, which Get-ItemProperty provides for all subkeys, targeted with a wildcard pattern, HKCU:\Network\*

    • Get-ItemProperty, when not given a property name, returns all properties - which in the case at hand are registry values - as a "property bag", in the form of a [pscustomobject] instance, with the name of the containing key reported in the .PSChildName property.

Unfortunately, working with PowerShell's registry provider is often not as straightforward as one would like.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, Dear for your detailed explanation. I really appreciate it. So far below code is working for me and changing the required values but it's showing the error also
$previousPath = "\\IAPC" $newPath = "\localhost" $nPath = HKCU:\Network* $getDriverLetter = Get-ItemProperty -Path HKCU:\Network* | Where-Object RemotePath | Select-Object PSChildName $getDriverPath =Get-ItemProperty -Path HKCU:\Network* | Where-Object RemotePath | Select-Object RemotePath foreach ($item in $getDriverPath) { $nitem = $item -replace('@{RemotePath=','') -replace('}}','') $newPath = $nitem -replace($oldServer,$newServer) Set-ItemProperty -Path HKCU:\Network* -Name RemotePath -Value $newPath } Error: The term 'HKCU:\Network*' is not recognized as
0

@imtiaz Hey i am not sure if you are trying the same but here is how i have achieved recently.

$oldServer = "\\abc.local" 
$newServer = "\abc.com"
$paths = REG QUERY HKCU\Network | where{$_ -ne ""}
foreach ($item in $paths)
{
  $oldPath = REG QUERY $item /f RemotePath /t REG_SZ | Out-String
  $oldPath1 = $oldPath.Split()[-12]
  $updatedPath = $oldPath1 -replace $oldServer,$newServer
  reg add $item /v RemotePath /t REG_SZ /d $updatedPath /f /reg:64
}

I tried before Get-ItemProperty and set-itemproperty was facing some issues. This might help you. I know this's not a "professional way" but this worked for me.

1 Comment

Thanks, solution share @mklement0 is the perfect solution. I have tried before your shared one but I'm afraid it will mess with values where remote path values are long. Anyways, thanks and welcome to the forum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.