Help needed on Powershell script to modify entries in .json backup: change URL matching type from global to hostname

New user here and I have a lot of passwords imported into bitwarden. Even though the global default of base domain URL-matching works, a lot of say “problematic” entries should be converted to hostname-matched ones.

I created a folder for these entries in the vault and imported them there. Additionally, I’ve asked ChatGPT to provide a powershell script to import a vault .json backup, iterate through its entries and for all those that exist in the special folder, modify value match from null (signifying follow the global default) to 1 (follow hostname-type URL matching).

Not a coder here, hence for relying on ChatGPT. It made a couple of errors but produced what seems to work regarding looping among the vault entries. The code is the following:

# Define the path to the exported Bitwarden JSON file
$vaultFilePath = ".\original.json"

# Read the JSON file and convert it to a PowerShell object
$vaultContent = Get-Content $vaultFilePath | ConvertFrom-Json

# Define the target folder ID that we want to match
$targetFolderId = "xxxxx-xxxxxxxx-xxxxxx-xxxxxxxxxx"

# Flag to track if changes are made
$changesMade = $false

# Iterate through each entry in the vault
foreach ($entry in $vaultContent.items) {

    # Check if the entry has the target folder ID
    if ($entry.folderId -eq $targetFolderId) {

        # Check if the match property is null
        if ($entry.login -ne $null -and $entry.login.uris -ne $null) {
            if ($entry.login.uris.match -eq $null) {
                # Set match to 1, to convert from a "follow global defaults" 
                # match to a hostname-based one
                $entry.login.uris.match = 1
                $changesMade = $true
                
                Write-Host "Updated Entry: $($entry.name) - Match set to 1"
            }
        }
        
    }
}

# If changes are made, save the updated JSON back to the file
if ($changesMade) {
    # Convert the PowerShell object back to JSON
    $updatedVaultContent = $vaultContent | ConvertTo-Json -Depth 10

    # Write the updated JSON back to the file (optional: backup original file first)
    $vaultFilePathBackup = $vaultFilePath + ".bak"
    Copy-Item -Path $vaultFilePath -Destination $vaultFilePathBackup -Force
    Set-Content -Path $vaultFilePath -Value $updatedVaultContent
    
    Write-Host "Changes saved to vault and backup created."
} else {
    Write-Host "No entries were updated."
}

targetFolderId is UID of the folder that contains the entries to be changed. Running this script on a the .json vault backup (named original.json) throws an error when trying to set match to 1, specifically:

The property 'match' cannot be found on this object. Verify that the property exists and can be set.

I’d be grateful if someone proficient, possibly from Bitwarden could throw some insight on why this thing does not work… If I comment this line out, the script seems to work just fine.

Any ideas?

Solution was changing line

$entry.login.uris.match = 1

to

$entry.login.uris | Add-Member -Force -MemberType NoteProperty -Name match -Value 1

Reimported the new .json (after deleting the existing data, to avoid double entries) and everything went fine :slight_smile:

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