Write to Registry

Welcome Forums General PowerShell Q&A Write to Registry

  • This topic has 4 replies, 3 voices, and was last updated 1 month ago by
    js
    Participant
    .
Viewing 4 reply threads
  • Author
    Posts
    • #224244
      Participant
      Topics: 10
      Replies: 11
      Points: 61
      Rank: Member

      Am trying to get the list of mapped drives on the users device and write that to registry under HKCU

      Get-WmiObject Win32_MappedLogicalDisk | Select Name, providername | New-ItemProperty -Path “HKCU:\ABCDE” -Name DriveMap -Value $_.Name

      It errors out. So need to write the Name and Providername as a entry in to the registry.

      Any help ?

    • #224265
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Vinod, when you post code or error messages or sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      What’s the complete error message? If you have more than one mapped drive you have to format the output to fit to a RegSz value or you have to change the registry key type.

    • #224268
      Participant
      Topics: 10
      Replies: 11
      Points: 61
      Rank: Member

      Thanks. the following is the error

      New-ItemProperty : The input object cannot be bound to any parameters for the command either because the command does
      not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
      At line:1 char:70
      
    • #224274
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Something like this should work:

      $DesitnationPath = 'Registry::HKEY_CURRENT_USER\ABCDE\DriveMap'
      if (-not (Test-Path -Path $DesitnationPath)) {
          New-Item -Path $DesitnationPath -ItemType Directory
      }
      
      Get-WmiObject Win32_MappedLogicalDisk | 
          ForEach-Object {
              New-ItemProperty -Path $DesitnationPath -Name $_.Name -Value $_.providername
          }
    • #224508
      js
      Participant
      Topics: 29
      Replies: 813
      Points: 2,450
      Helping Hand
      Rank: Community Hero

      You’re piping both name and providername new-itemproperty, but there’s no -providername parameter, and you’re using -name a second time. You can only use $_ inside a scriptblock. I don’t know what DriveMap is. This way, I’m turning the providername property into a value property.

      get-wmiobject win32_mappedlogicaldisk | select name,@{n='value';
        e={$_.providername}} | new-itemproperty -path hkcu:\abcde
      
      Z:           : \\powershell.org\riches
      PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\abcde
      PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER
      PSChildName  : abcde
      PSDrive      : HKCU
      PSProvider   : Microsoft.PowerShell.Core\Registry

      Unfortunately, this doesn’t work. I think because -value is of type object? It literally writes “$_.providername” in the registry.

      get-wmiobject win32_mappedlogicaldisk | select name,providername | 
        new-itemproperty hkcu:\abcde -value { $_.providername }
      • This reply was modified 1 month ago by js.
      • This reply was modified 1 month ago by js.
      • This reply was modified 1 month ago by js.
      • This reply was modified 1 month ago by js.
      • This reply was modified 1 month ago by js.
Viewing 4 reply threads
  • You must be logged in to reply to this topic.