Menu

PowerShell CSV to V2 XML converter

Paul
2017-03-17
2023-09-27
1 2 > >> (Page 1 of 2)
  • Paul

    Paul - 2017-03-17

    PowerShell CSV to V2 XML Converter

    Import custom fields and groups

    A CSV to XML converter may seem redundant, but the KeePass generic CSV importer is fussy with groups, doesn't handle additional fields and doesn't have the ability to set Tags, so I've come up with this converter for those who need those features.

    Requirements / Conventions

    • CSV files MUST be encoded as UTF-8 to retain special characters.
    • The converter doesn't require any specific column headings. Any non-standard KeePass fields will be created.
    • Use the default KeePass field names to make life easier. Title, Username, Password, URL, Notes
    • Quotes in passwords must be escaped. Adding an additional quote in front of the required quote is sufficient - see the example CSV below.
    • If you use groups / sub-groups you must specify the full path of each entry using a backslash (\) as the group separator. No data in the group column will result in the entry being placed in the root of the imported data.
    • You can use a "Tag" column to tag types of entries, e.g. Banking, Email, etc. Multiple tags can be specified using a delimiter of backslash, comma, colon or semicolon.
    • Imported data will be placed in a group called "Imported Data".
    • An additional group called "Delete Me" will be created by the import procedure. This is an empty group and can be deleted.


    Note: How to run a PowerShell Script (no longer required).
    MS Technet.article

    Use
    1. Create / import your data from your existing password manager to a spreadsheet.
    2. Set up the first row with column headings that reflect the location of your fields. See the example below.
    3. Save the spreadsheet as a CSV file.
    4. Extract the 2 files from the attached zip to a new folder.
    5. Run (double click) on the CSV2XML Command Script to launch the converter.
    It may trigger Windows smart screen - click “More Info” and then “Run anyway”.
    6. Select the CSV file and choose an output file.
    7. Import the resulting output (KeePass V2 XML) file into KeePass.

    Example CSV (based on an mSecure export)
    The first line is the column headers.

    Group,Tag,Title,Notes,username,password,Name,Branch,Phone No,Code,URL,ID,Notes2,IconID,ExpiryTime
    Historical Society,Bank Accounts:Finance,PayPal - 29 Palms Historical,,donations@29palmshistorical.org,crelfg5948,Anonymouse,NY,1234,,www.paypal.com,,Customer Svc. Pin: 205679
    Microsoft,Internet,MSDN,Some Note,bill@microsoft.com,richbastard,,,,,,,,
    \,,Complex,,Password,"123"">$,&\",,,,,www.home.com,,,
    Root\subgroup,,Sub Group 2 levels,,subby,ghwretuirew045#';~@:,,,,www.subby.com
    Root\subgroup\extra,,Sub Group 3 levels,,subby,ghwretuirew045#';~@:,,,,www.subby.com
    ,,No group specified Entry,Some notes,me,mypass%$&\^&)((&$%$%£!,,,,,http://keepass.info,,,0,202101251453
    


    Column names with specific meaning
    Group: Group structure
    Title: Entry title
    Username: Username
    Password: Password
    Tag, Tags: Single or backslash, comma, colon, semicolon separated tags
    Notes: Notes
    Iconid: Internal KeePass icon used for the entry
    ExpiryTime: Time the entry expires / has expired, in local time. Format: YYYYMMDDHHMM or YYYYMMDD


    The PowerShell file and these instructions are in the attached zip file.

    Note: There is now a direct CSV to KDBX converter here.

    cheers, Paul

     

    Last edit: Paul 2024-06-15
  • Dominik Reichl

    Dominik Reichl - 2017-03-18

    Great, thanks! :-)

    I've added it to the plugins/extensions page:
    http://keepass.info/plugins.html#convertto2xxml

    Best regards,
    Dominik

     
  • TwoHawks

    TwoHawks - 2017-03-28

    Thank you very much for creating this script. Unfortunately, when I run this (XP environment with PS2) I only get the group called "Delete Me" and nothing else. I even created your simple example -same result. I am guessing this requres newer versions of windows only, or?
    Thanks againn.

     

    Last edit: TwoHawks 2017-03-28
  • Paul

    Paul - 2017-03-28

    It may require a later version of PS or .NET - I don't have XP to test.

    Can you post the output from the sample data?

    cheers, Paul

     
  • TwoHawks

    TwoHawks - 2017-03-28

    Thanks, Paul. Here you go...

    PS C:> P:\CSV2XML.ps1
    Import-Csv : A parameter cannot be found that matches parameter name 'encoding'.
    At P:\CSV2XML.ps1:72 char:32
    + $csvFile = Import-CSV -encoding <<<< default $CSVFileName
    + CategoryInfo : InvalidArgument: (:) [Import-Csv], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ImportCsvCommand

    Get-Member : No object has been specified to the get-member cmdlet.
    At P:\CSV2XML.ps1:100 char:37
    + $columnNames = $csvFile | get-member <<<< -membertype NoteProperty
    + CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

    Cannot index into a null array.
    At P:\CSV2XML.ps1:101 char:14
    + if ($csvFile[ <<<< 0].Group -ne $null) { # Group Column Exists
    + CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

     
  • TwoHawks

    TwoHawks - 2017-03-28

    Duplicate post

     

    Last edit: TwoHawks 2017-03-28
  • TwoHawks

    TwoHawks - 2017-03-29

    Found it... https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/import-csv
    ...encoding param for Import-CSV was introduced in PowerShell 3
    When I removed it, the script ran my tests without error. I'll see how it goes with the real deal.

    The change for me is Line 72:
    FROM:
    $csvFile = Import-CSV -encoding default $CSVFileName
    TO:
    $csvFile = Import-CSV $CSVFileName

    Cheers,
    HTH

     
  • TwoHawks

    TwoHawks - 2017-03-29

    For the sake of being complete, folks should also be aware of the following report:
    https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/17460421-get-content-bug-a-parameter-cannot-be-found-that
    ...which, while its a "Get-Content" bug, I thought it may have been my issue since I was trying to run the script on a virtual map. My test above seems to run not only on the local, but also on my mapped zone, so it may not be an issue, but I need to run more tests to be sure (there are several types of mapping one can setup).
    - HTH -

     
  • TwoHawks

    TwoHawks - 2017-03-29

    I really do not mean to flood your discussion zone, Paul, but FWIW (and because I am grateful and think this may be helpful for others).... I was searching all over before I accidentally found this script ...by frustratingly -finally reading through ALL the plugins posted for Keepass because I could not find what I was after. Then I identified this asa potential solution.

    So If I may be so bold to suggest adding to your description...
    Scripts to convert files created by other applications to KeePass 2.x XML files.
    ...something like this second line...
    Import custom delimited fields for creating custom Entry or entries in keepass.

    If that second line had been in your description I would have found this right away, because I was trying to figure out how to get a set of custom fields from, say, a delimited file (txt or csv) into Keepass without having to painstakingly, and every time I wanted to import, try to use the "Add field" tool in Keepass' Generic CSV Importer (on the "Structure" tab) to add, assign, and sort custom fields ...and for every single field I wanted.

    Hope that makes sense. Your script here tackles that, and could enable some interesting flexibility in Keepass while also making it truly easy.

    In my case, I am experimenting with exporting business addresses (of support entities) related to saved passcode entries --for a more one stop lookup while working.
    In this regard, I found Mitch's "Entry Templates" for Keepass that facilitates using Keepass for addressbook style entries in a very reasonably affectual way.

    Cheers,
    HTH

     
  • Paul

    Paul - 2017-03-29

    Thanks for the feedback - I wish everyone was this keen. :)

    The encoding parameter is to fix an issue I found on my machine where some characters are not imported correctly - the pound sign in the 3rd example.
    Is the password valid in the XML?

    cheers, Paul

     
  • TwoHawks

    TwoHawks - 2017-03-29

    I'll be checking that tonite or tomorrow as time permits and get back to you. (and thank you for the kind remark)

     
  • TwoHawks

    TwoHawks - 2017-04-02

    Yes, happens to be valid here, even though my configuration does not support the encoding param.
    The xml header does not list any encoding, so no telling by that, however, notepad++ reports the encoding as "UTF8 w/o BOM".

    Also interesting, when reviewing a straight Keepass import of the csv, the csv encoding is listed only as western euro windows, and if I toggle into ascii (which is a subset of utf8), I lose a character ("£" ), and if I toggle into utf8 I get a placeholder for that character, and when I toggle into utf7 everything remains the same ('correct'). Don't know if any of that may help, but I figured I'd observe and report ;^)
    Cheers,
    Twohawks

     

    Last edit: TwoHawks 2017-04-02
  • Paul

    Paul - 2017-06-12

    Update file to run in PS V2 as well as later versions.

    cheers, Paul

     
  • Annie O.

    Annie O. - 2017-10-06

    Thanks for this script Paul. As a non-technical person I had an interesting time with Google trying to find how to find/execute Power Shell and then to set the permissions to allow scripts to run. Perhaps you may wish to add some instructions to the accompanying .zip file.

    Two minor issues that I found and (procedurally) worked around:

    • When Notes have a new line (\n) the XML importer ignores that and imports as text. The Generic CSV importer treats it properly. My workaround was to use NotePad++ and global change '\n\ to ' '.

    • Having two groups in the same import file, e.g., \Root\Active\General and \Root\Inactive\General, produces the following tree:

    • Root
    • Active
    • General
    • General

    Workaround is to manually create the Active/Inactive brances and drag/drop the General folders appropriately.

    Attached are CSV and XML files with the above cases.

    Thanks again for the great work.

     
  • Paul

    Paul - 2017-10-06

    Thanks for the feedback, I'll have a look over the next few days (hopefully).

    cheers, Paul

     
  • Annie O.

    Annie O. - 2017-10-06

    No rush Paul, my comments are mainly an FYI. Using my workarounds above I am now fully migrated from mSecure to Keepass. Been using the former since January 2010 but it just stopped syncing in September.

    Between Keepass and Keepass2Android, once the learning curve flattened, made it painless. My regards to you and Dominik for a great product.

     
  • Paul

    Paul - 2017-10-14

    Latest update now at the top of this thread.
    Changes:
    Convert "\n" in text to CR/LF
    Updated readme file

    Bug fixes
    * Sub-groups with matching child groups are now handled correctly

    cheers, Paul

     
  • Unity001

    Unity001 - 2018-09-02

    Hey Paul,
    I tried using this converter but it doesn't seem to work.
    When I convert a CSV to XML, inside the output file I see this:

    <?xml version="1.0"?>
    <KeePassFile>
      <Root>
        <Group>
          <Name>Delete Me</Name>
          <Notes />
          <IconID>48</IconID>
          <Group>
            <Name>Imported Data</Name>
            <Notes />
            <IconID>48</IconID>
          </Group>
        </Group>
      </Root>
    </KeePassFile>
    

    The CSV itself, though, has a lot of custom fields and rows.
    What could be the problem?
    Maybe you could attach a template CSV that works for you?

     
  • Paul

    Paul - 2018-09-02

    The implication is that the converter can't find the data / headers.
    The sample in the first post here is the required format. Maybe you could post a couple of lines of your CSV for me to look at.

    cheers, Paul

     
  • Gaspard

    Gaspard - 2021-01-02

    Hello Paul and thanks for this nice converter.
    Would you kindly enhance it and allow to specify the IconID of an entry?
    I suggest that if the CSV contains a column named "IconID", by convention you take the value in this column to specify the icon (rather than create a custom field).
    Thanks.
    Gaspard

     
  • Gaspard

    Gaspard - 2021-01-02

    By the way, it would be nice to set the expiration date in the same way.

     
  • Paul

    Paul - 2021-01-03

    Wow, that's pretty specific for an import utility, but not a problem to do.

    The icon ID is specific to KeePass so the assumption is that the CSV was created from a KeePass database. Why not use the database in the first place?

    What format do you want for the expiry date? I suggest YYYYMMDD or YYYYMMDDHHMM.
    KeePass stores the date as UTC and you probably expect local time at import?

    cheers, Paul

     
  • Gaspard

    Gaspard - 2021-01-03

    Thank you Paul,
    The notification ended up in the SPAM, sorry for responding only now.

    I am converting SplashID 3.22 data from my beloved old but still working Palm T3. I am reorganising it in Excel before feeding it into KeePass, and I specify icons with excel formula.
    But concerning the icon, don't worry: I studied your script, took the opportunity to learn some basis of Powershell (I have quite some programming background with other languages but that was more than 25 years ago...). And I managed to add the icon thanks to the clarity of your code (see attachment, my modification are indicated with ##GG).

    I haven't investigated (yet?) the expiry date. For the format of the date, any format that Excel can save in a CSV would be OK.

    Btw, I have a small issue: your script converts newlines inside values with " " (ampersand Hash x B semicolon) . I fix the issue with a global Find/Replace in a plain text editor, but it is annoying. (the file I feed your script with is a UTF-8 in PC format).

    Cheers,
    Gaspard

     

    Last edit: Gaspard 2021-01-03
  • Paul

    Paul - 2021-01-04

    Can you post a sample of a line where the newline conversion fails?

    cheers, Paul

     
  • Gaspard

    Gaspard - 2021-01-04

    Here you are.
    Well, I realise that the newline generated by SplashID export to csv is not a real newline, but a special character. It is treated transparently by Excel and by the text editor I use to make sure I have an UTF-8 encoded file.
    Thanks and cheers,
    Gaspard

     
1 2 > >> (Page 1 of 2)

Log in to post a comment.