Sort Vault Items by Revision Date Using the CLI

Sort vault items using the Bitwarden CLI along with a tool called jq with the following command:

bw list items | jq ‘. |= sort_by(.revisionDate)’

Courtesy of @Chris_Magnuson
Fyi, here is a PowerShell option for reviewing recently modified passwords vault items:

(.\bw list items | ConvertFrom-Json) | 
Select-Object -Property Name, RevisionDate | 
Sort-Object -Property RevisionDate | 
Select-Object -First 10

Edit: The above post was revised on 2023-01-30, to make corrections to the PowerShell script. In addition, alternatives to the above PowerShell expression have been posted below (here and here). [Edit by @grb]

For the benefit of those users who new to these tools, the jq tool can be downloaded here, and instructions for downloading and installing the Bitwarden CLI are available here.

The PowerShell method does not require installation of the jq tool, only the CLI. To open a Windows PowerShell console in Windows 10, just right-click the Start Menu button (Windows icon) and select “Windows PowerShell”. Instructions for accessing PowerShell in Windows 11 are available here.

I can use some help with the PowerShell option. Ideally, I can create a script to run and output the sort in order to track a large number of password changes.

In PS, when I execute the commands in a single line–I stringed the command together to look like this: .\bw list items | ConvertFrom-Json | Select-Object -Property Name, RevisionDate | Sort-Object -Property RevisionDate -Descending | Select-Object -First 10.

After entering the mater pw, I got a blank Name and RevisionDate, but no entries. Any ideas why?

I’m looking for a easy solution to run this as I make changes. I’d appreciate any help.

What happens if you just do bw list items? Do you see all of your vault contents scroll by in JSON format?

I do see all the vault contents scroll by in JSON format when I do “bw list items”.

When I use “.\bw list items | ConvertFrom-Json | Select-Object -Property Name, RevisionDate | Sort-Object -Property RevisionDate -Descending | Select-Object -First 10”, I get: “Name RevisionDate” but nothing underneath it.

Thanks for your help.

So the command bw list items works, but the command that starts with .\bw list items doesn’t work. How about removing the first two characters (.\) from the second command?

Sorry, I should have been more clear, in PS, I always have to use .\ to run an executable. So in this case, the full command was “.\bw list items”. Otherwise, PS errors with “the term ‘…bw’ is not recognized”

OK, I understand. When debugging code on a forum, it is best to share the expressions verbatim. Even better to enclose the code snippets using the “preformatted text” format (click on </> button in editor, or type ` at the start and end of the source code snippets).

So, if I understand correctly, the following expression works:
.\bw list items

Let’s try to expand on it:

.\bw list items | ConvertFrom-Json

Do you still see your vault contents scroll by?

Thanks for your reply. Sorry I was unclear before. I am not a coder, so this is new stuff.

Yes, when I execute “.\bw list items | ConvertFrom-Json”, I do see my vault contents scroll by with revision date and creation date.

OK, great, now try this:

.\bw list items | ConvertFrom-Json | Select-Object -Property Name, RevisionDate

Do you still see the vault contents scroll by?

I no longer see the vault contents scroll by. Just two columns Name and RevisionDate with nothing underneath.

OK, try the following:

.\bw list items | ConvertFrom-Json | Select-Object -First 1

What column names to you see at the top? Please copy & paste verbatim from the PowerShell output into the forum editor (and preferably enclose the pasted text by a pair of ` separators).

Please see screenshot. Thank you.

Sorry, I misunderstood. When I execute:

.\bw list items | ConvertFrom-Json | Select-Object -First 1

I see the list of my entire vault scroll by. It exceeds the buffer, so when I scroll up, I do not see the column names at the top, but the screenshot is a sample of what the entries look like:

OK, it’s a bit challenging for me to debug this, because I don’t have the CLI installed myself, so I can’t do my own testing. I did find a site that showed sample output of the bw list items command:

[{"object":"item","id":"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa","organizationId":null,"folderId":null,"type":1,"reprompt":0,"name":"post-test","notes":null,"favorite":false,"login":{"username":"ryan","password":"terrible-password","totp":null,"passwordRevisionDate":null},"collectionIds":[],"revisionDate":"2021-11-30T11:44:25.846Z"}]

Using this, I am able to reproduce your results (i.e., it apparently doesn’t work).

I conclude that either the format of the output of the bw list items CLI command or the behavior of the ConvertFrom-Json PowerShell commandlet has changed since March, or perhaps @Chris_Magnuson proposed the above command sequence on the theory that it would work, but without actually testing it.

Hopefully, someone with more experience using the CLI (and/or PowerShell) can step in to debug this further.

Update (2023-01-30):

@snovvman The code originally proposed by @Chris_Magnuson can be fixed by inserting “.\” before the “bw” command, by using parentheses to enclose the expression “.\bw list items | ConvertFrom-Json”, and by removing the parameter “-Descending” from the sorting command. The top post has now been edited to incorporate these corrections. In addition, alternative PowerShell expressions have been proposed below (here and here).

1 Like

Thank you for helping and patiently going through the troubleshooting process. I’m just hoping that the sort feature will be baked into BW soon.

1 Like

Not a powershell expert, but execute each one of these as a separate command in a PS terminal and it works

$e = .\bw list items
$f = $e | ConvertFrom-Json
$f | Sort-Object -Property RevisionDate -Descending | Select-Object -Property name, revisionDate, passwordhistory

I added in passwordHistory so that I could see ones that I’ve recently updated.

2 Likes

@Bill_Morgan Welcome to the forum, and thank you for this valuable contribution!

Based on your suggestion, I have come up with the following PowerShell script to find the oldest passwords in one’s vault:

param ($n=10)
$x =  .\bw list items
$y = $x | ConvertFrom-Json
$z = $y | Select-Object -ExpandProperty login -Property name
$z | Sort-Object -Property passwordRevisionDate | Select-Object -Property name,passwordRevisionDate -First $n

Copy and paste the above into a text file, renaming it oldest.ps1 and save it in a folder on the PowerShell path. Then type the following to list the 25 oldest passwords (for example):

.\oldest 25

Just typing .\oldest without specifying a number should list the oldest 10 passwords by default (if you wish, you can change this default behavior by modifying the number shown in the first line of the script).

Alternatively (e.g., if you are having with the path or with permissions for running scripts), just copy and execute each line of the above code individually, except that the first line should just be $n = 25 (or whatever number of passwords you want to list) without the “param” keyword or the parentheses.

Caveats:

  • I have tested this only by hardcoding the sample output of .\bw list items that I had found above, as I don’t have the Bitwarden CLI client installed.
  • I know very little about PowerShell scripting (and I learned something new from @Bill_Morgan today!), so the code above may be inelegant. However, I am 90% sure it will work! :sweat_smile:
    Please let me know if this works for you, or if you have any problems with this script.

Edit:

Inspired by this recent post by @tijama, I was able to convert my script above to a one-liner which can be copy-pasted directly into the PowerShell terminal (without creating a script):

(.\bw list items | ConvertFrom-Json) | Select-Object -ExpandProperty login -Property name | Sort-Object -Property passwordRevisionDate | Select-Object -Property name,passwordRevisionDate -First 10

The number 10 at the end of the line can be modified (it represents the number of items that will be displayed), or you can omit the -First 10 altogether to list all vault items (sorted in order of the most recent password revision).

1 Like

Thank you for this! I had recently converted from LastPass and like other’s wanted to be able to keep track of which passwords I had changed and which I had not. Two observations, entries that had been converted from lastpass and not edited do not have a date. This makes sense because there has been no revision. Also, I had errors at the beginning of the output, these seemed to be from secure notes (not login’s).

1 Like

Hey @rts yes, this is caused because Bitwarden has a 10,000 character limit currently for notes, so large ones need to be broken up into smaller notes, for more information, see: https://bitwarden.com/help/import-from-lastpass/#import-troubleshooting