Write to Registry

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 ?

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.

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

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
    }

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 }