Imtiaz Ali
Imtiaz Ali

Reputation: 41

Filter PowerShell command Output

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)

Upvotes: 0

Views: 330

Answers (2)

mklement0
mklement0

Reputation: 440092

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.

Upvotes: 1

BU MS
BU MS

Reputation: 1

@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.

Upvotes: 0

Related Questions