regedit

repository·master·Indexed 43 days ago

https://github.com/kessler/node-regedit

A Node.js library for reading, writing, and manipulating the Windows Registry using the Windows Script Host without requiring native code. Version 5.1.4 provides a callback-based API and a .promisified property for async/await usage. It supports operations such as list(), putValue(), createKey(), and deleteKey(), with specific support for registry architectures (32-bit and 64-bit) and a streaming interface for large datasets.

Tokens
3.1K
Snippets
11
Records
14
Agent score
5%

What's inside regedit

  1. Use the Promise-based API

    master

    By default, regedit uses a callback-based API. To use async/await or Promises, import the .promisified property.

    // callback api
    const regedit = require('regedit')
    
    // promise api
    const regedit = require('regedit').promisified
    const regedit = require('regedit').promisified
  2. Configure external VBS location for Electron apps

    master

    Because regedit executes .wsf files via Windows Script Host, these files cannot be accessed if they are bundled inside an Electron ASAR archive. You must move the .wsf files outside the ASAR and tell regedit where they are using setExternalVBSLocation(location).

    // Assuming the files lie in <app>/resources/my-location
    const vbsDirectory = path.join(path.dirname(electron.remote.app.getPath('exe')), './resources/my-location');
    regedit.setExternalVBSLocation(vbsDirectory);
    // Assuming the files lie in <app>/resources/my-location
    const vbsDirectory = path.join(path.dirname(electron.remote.app.getPath('exe')), './resources/my-location');
    regedit.setExternalVBSLocation(vbsDirectory);
  3. Manage registry architecture access

    master

    When working on 64-bit Windows, you may need to specify which registry view you are accessing. regedit provides several ways to handle this:

    1. Via architecture parameter: Pass one of the following strings to the standard API methods:

      • A (OS_ARCH_AGNOSTIC): Access the registry without a specific architecture (on a 32-bit process on a 64-bit machine, HKLM\Software will resolve to HKLM\Software\wow6432node).
      • S (OS_ARCH_SPECIFIC): Automatically determine the architecture.
      • 32 (OS_ARCH_32BIT): Access the 32-bit registry view.
      • 64 (OS_ARCH_64BIT): Access the 64-bit registry view.
    2. Via regedit.arch namespace: Provides shorthand methods that omit the architecture parameter by fixing it to a specific view:

      • regedit.arch.list(keys, callback) (uses S)
      • regedit.arch.list32(keys, callback) (uses 32)
      • regedit.arch.list64(keys, callback) (uses 64)
    3. Via regedit.arch.promisified: Provides Promise-based versions of the arch shorthand methods (e.g., regedit.arch.promisified.list32(keys)).

  4. List registry keys and values with regedit.list()

    master

    Lists the direct content (sub-keys and values) of one or more registry paths. You can pass a single string or an array of strings to batch multiple queries in one run.

    Result Structure

    The result is an object where each key is the registry path provided. Each path contains:

    • exists: (boolean) whether the path exists.
    • keys: (Array<string>) direct sub-keys.
    • values: (Object) an object of value names containing { value, type }.

    Accessing Default Values

    In the Windows registry, a key may have a default value. In the returned object, the default value is identified by an empty string ('') as the value name.

    // Accessing the default value
    regedit.list('path\\to\\key', function (err, result) {
        var defaultValue = result['path\\to\\key'].values[''].value
    })
    const regedit = require('regedit').promisified
    
    async function main() {
      const listResult = await regedit.list('HKCU\\SOFTWARE')
      console.log(listResult)
    }
  5. Create, Delete, and Manipulate registry keys

    master

    Use the following methods to manage registry keys:

    • regedit.createKey([String|Array], [Function]): Creates one or more keys.
    • regedit.deleteKey([String|Array], [Function]): Deletes one or more keys.
    • regedit.deleteValue([String|Array], [Function]): Deletes one or more values.

    Note: These operations mutate the keys array.

  6. Use the regedit.list() streaming interface

    master

    For large amounts of data or to avoid command-line argument length limitations, use the streaming interface. This interface emits 'data' events for each entry and a 'finish' event when complete.

    Note: This operation will mutate the keys array.

    regedit.list(['HKCU\\SOFTWARE', 'HKLM\\SOFTWARE'])
    .on('data', function(entry) {
        console.log(entry.key)
        console.log(entry.data)
    })
    .on('finish', function () {
        console.log('list operation finished')
    })
  7. Force registry architecture with regedit.arch

    master

    When running in a 32-bit process on a 64-bit OS, registry paths may be redirected to wow6432node. Use the .arch methods to explicitly target specific architectures.

    • regedit.arch.list32(): Forces 32-bit architecture.
    • regedit.arch.list64(): Forces 64-bit architecture.
    • regedit.arch.list(): Automatically selects based on system architecture.

    These methods support both callback and streaming interfaces.

  8. List unexpanded values with regedit.listUnexpandedValues()

    master

    Lists the values of one or more value keys without expanding embedded environment variables (e.g., it will return %USERPROFILE% instead of the actual path). This is useful for inspecting raw registry data.

    This API uses a wshell object RegRead method and supports both callback and streaming interfaces.

    const regedit = require('./index').promisified
    
    async function main() {
      const res = await regedit.listUnexpandedValues('HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\AppData')
      console.log(JSON.stringify(res, null, '\t'))
    }
    
    main()
  9. Write values to the registry with regedit.putValue()

    master

    Writes one or more values to the registry. The input object structure should mirror the output of regedit.list().

    Supported Types

    • REG_SZ, REG_EXPAND_SZ: String
    • REG_DWORD, REG_QWORD: JavaScript Number
    • REG_MULTI_SZ: Array of strings
    • REG_BINARY: Array of numbers (bytes)
    • REG_DEFAULT: Used for setting the key's default value. Note: The value type for REG_DEFAULT must be REG_SZ and the name provided in the object is insignificant.
    var valuesToPut = {
        'HKCU\\Software\มายSoftware': {
            'myValue1': {
                value: [1,2,3],
                type: 'REG_BINARY'
            },
            'myValue2': {
                value: 'aString',
                type: 'REG_SZ'
            }
        }
    }
    
    regedit.putValue(valuesToPut, function(err) {
        // handle error
    })
    var valuesToPut = {
        'HKCU\\Software\\MySoftware': {
            'myValue1': {
                value: [1,2,3],
                type: 'REG_BINARY'
            },
            'myValue2': {
                value: 'aString',
                type: 'REG_SZ'
            }
        },
        'HKCU\\Software\\MySoftware\\foo': {
            'myValue3': {
                value: ['a', 'b', 'c'],
                type: 'REG_MULTI_SZ'
            }
        }
    }
    
    regedit.putValue(valuesToPut, function(err) {
        // handle error
    })
  10. Create, Delete, and Put registry values

    master

    Use these methods to manipulate the registry structure and data:

    createKey(keys, [architecture], callback)

    Creates a new registry key at the specified path(s). keys can be a string or an array.

    deleteKey(keys, [architecture], callback)

    Deletes the specified registry key(s).

    deleteValue(keys, [architecture], callback)

    Deletes a specific value within a key. keys should be the path to the key.

    putValue(map, [architecture], callback)

    Sets values in the registry. The map object follows this structure:

    {
      "KEY_PATH": {
        "VALUE_NAME": {
          "type": "REG_SZ | REG_BINARY | REG_MULTI_SZ | REG_DWORD",
          "value": "actual_value"
        }
      }
    }

    Note: For REG_BINARY, the value must be an array of numbers. For REG_MULTI_SZ, the value must be an array of strings.

    const regedit = require('regedit');
    
    // Create a key
    regedit.createKey('HKEY_CURRENT_USER\\Software\\MyApp', 'S', (err) => {
      if (err) console.error(err);
    });
    
    // Put a value
    const map = {
      'HKEY_CURRENT_USER\\Software\\MyApp': {
        'Version': { type: 'REG_SZ', value: '1.0.0' },
        'Flags': { type: 'REG_DWORD', value: 1 }
      }
    };
    regedit.putValue(map, 'S', (err) => {
      if (err) console.error(err);
    });
  11. List registry keys and values

    master

    The list method retrieves registry information. It supports two interfaces:

    Callback Interface

    regedit.list(keys, [architecture], callback)

    • keys: A string or an array of strings representing the registry paths to list.
    • architecture: (Optional) One of 'A', 'S', '32', or '64'. If omitted, defaults to 'A'.
    • callback: A function called with (err, result).

    Streaming Interface

    If no callback is provided, regedit.list returns a through2 object stream. This is useful for processing large amounts of registry data without loading everything into memory at once.

    regedit.listUnexpandedValues(valuePaths, [architecture], callback) works similarly but is used to read unexpanded values from specific paths.

    const regedit = require('regedit');
    
    // Callback usage
    regedit.list('HKEY_LOCAL_MACHINE\\Software', '64', (err, res) => {
      if (err) throw err;
      console.log(res);
    });
    
    // Streaming usage
    const stream = regedit.list(['HKEY_CURRENT_USER\\Software']);
    stream.on('data', (data) => {
      console.log('Received chunk:', data);
    });