powershellregistry

Write registry value into textfile using powershell


I'm tring to export the open notebook fielpaths from OneNote into a text file. But no matter what I try, I can't get it to work. Am I missing something obvious here? There are definitly values in this registry path.

$registryPath = "HKCU:\SOFTWARE\Microsoft\Office\16.0\OneNote\OpenNotebooks"
$filePath = [Environment]::GetFolderPath("MyDocuments")
$outFile = Join-Path -Path $filePath -ChildPath "OneNotePaths.txt"

Get-ChildItem -Path $registryPath | ForEach-Object {
  $notebookPath = (Get-ItemProperty -Path $_.PsPath).Path
  Add-Content -Path $outFile -Value $notebookPath
}

Edit: the script somehow does not enter the ForeEach loop as if there ar no items found in the Path.


Solution

  • Edit: the script somehow does not enter the ForeEach loop as if there ar no items found in the Path.

    That's correct - because there are no child items under the OpenNoteBooks key - the listed notebooks are stored in values attached to the key itself. To get those values, use Get-ItemProperty (or Get-ItemPropertyValue):

    $openBooksKey = Get-Item $registryPath
    $openBooksKey.GetValueNames() |ForEach-Object {
      $openBooksKey |Get-ItemPropertyValue -Name $_ |Add-Content -Path $outFile
    }