1
votes

I am trying to write a powershell script that creates registry keys and their values from a csv file containing the list of the registries that are to be created.

The Problem is that i am getting an error. Only the registry keys are created and not their properties.


This is my csv file "listeRegistre.csv" :

Path,Name,Value,Type
"HKCU:\Software\Clients\StartMenuInternet",(Default),"[ProductName].exe",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe",(Default),"[ProductName]",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe",LocalizedString,"[ProductName]",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities",ApplicationDescription,"description",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities","ApplicationIcon","[ProgramFilesFolder][Manufacturer]\[ProductName]\[ProductName].exe,0",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities","ApplicationName","[ProductName]",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities\FileAssociation","","",""
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities\StartMenu","StartMenuInternet","[ProductName].exe",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities\URLAssociations","ftp","[ProductName]URL",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities\URLAssociations","ftps","[ProductName]URL",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities\URLAssociations","http","[ProductName]URL",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities\URLAssociations","https","[ProductName]URL",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities\URLAssociations","url","[ProductName]URL",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\DefaultIcon","(Default)","[ProgramFilesFolder][Manufacturer]\[ProductName]\[ProductName].exe,0",String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\InstallInfo","HideIconsCommand","[ProgramFilesFolder][Manufacturer]\[ProductName]\[ProductName].exe "--hideicons,String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\InstallInfo","IconsVisible",1,DWord
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\InstallInfo","ReinstallCommand","[ProgramFilesFolder][Manufacturer]\[ProductName]\[ProductName].exe" --reinstall,String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\InstallInfo","ShowIconsCommand","[ProgramFilesFolder][Manufacturer]\[ProductName]\[ProductName].exe" --showicons,String
"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\shell\open\command","(Default)","[ProgramFilesFolder][Manufacturer]\[ProductName]\[ProductName].exe",String
"HKCU:\Software\Classes\[ProductName]HTML\shell\open\command",,"",""

The headers of the csv file :

  • Path : path of the registry key to be created
  • Name : name of the property item to be created
  • Value : value of the property "Name"
  • Type : type of the property (REG_SZ OR DWORD)

Following a screenshot of my csv file

this is my csv file


This is the powershell script :

 $registries = import-csv "c:\registre\listeRegistre.csv"

    ForEach ($registry in $registries){

        $registryPath = $($registry.Path)
        $name = $($registry.Name)
        $value = $($registry.Value)
        $type = $($registry.Type)
        Write-host $registryPath $name $value $type

        #If the registry doesn't exist : creates it 
        #Else creates only the properties
        IF(!(Test-Path $registryPath)){
            New-Item -Path $registryPath -Force | Out-Null
            New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType $type -Force | Out-Null}
        ELSE {
            New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType $type -Force | Out-Null
        }

    }

The data is properly displayed on screen.

I am getting the following errors :

New-ItemProperty : Impossible de lier l'argument au paramètre « Name », car il s'agit d'une chaîne vide.
Au niveau de ligne : 15 Caractère : 51
+         New-ItemProperty -Path $registryPath -Name <<<<  $name -Value $value -PropertyType $type -Force | Out-Null
    + CategoryInfo          : InvalidData: (:) [New-ItemProperty], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.NewItemPropertyCommand

The translation of the first lines of the error : New-ItemProperty : can't associate Name to arguments as Name is empty.

Test-Path : Impossible de lier l'argument au paramètre « Path », car il s'agit d'une chaîne vide.
    Au niveau de ligne : 13 Caractère : 19
    +     IF(!(Test-Path <<<<  $registryPath)){
        + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

The translation of the first lines of the error : Test-Path : can't associate arguments to Path as Path is empty.

Following a screenshot of my script

This my script

Following the errors associated with my script :

errors part1

errors part2

Please could somebody help me, i am new in powershell and i am stuck very badly as this code works with one registry key like this :

$registryPath = "HKCU:\Software\ScriptingGuys\Scripts"

$name = "Version"

$value = "1"

$type = "DWORD"

IF(!(Test-Path $registryPath)){

    New-Item -Path $registryPath -Force | Out-Null

    New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType $type -Force | Out-Null}
ELSE {
$value = "5"

    New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType DWORD -Force | Out-Null
}

the above script works as the registry key and it's property both are created without problem.


So in response to the problem above i used the script posted by bert :

$registries = import-csv "c:\registre\listeRegistre.csv"

    ForEach ($registry in $registries){

        $registryPath = $($registry.Path)
        $name = $($registry.Name)
        $value = $($registry.Value)
        $type = $($registry.Type)
        Write-host $registryPath $name $value $type

        #If the registry doesn't exist : creates it 
        #Else creates only the properties
        IF(!(Test-Path $registryPath)){
            New-Item -Path $registryPath -Force | Out-Null
            New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType $type -Force | Out-Null}
        ELSE {
            New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType $type -Force | Out-Null
        }

    }

But when lauching this script i get lots of errors which i fail to understand,Please could you help me in this matter.

The errors i am getting :

Get-ItemProperty : Le membre « (default) » est déjà présent.
Au niveau de C:\registre\registry_scriptv3.ps1 : 15 Caractère : 24
+         if ((Get-ItemProperty <<<<  -Path $Path -Name $Name) -ne $Null)
    + CategoryInfo          : NotSpecified: (:) [Get-ItemProperty], ExtendedTy
   peSystemException
    + FullyQualifiedErrorId : AlreadyPresentPSMemberInfoInternalCollectionAdd,
   Microsoft.PowerShell.Commands.GetItemPropertyCommand

Remove-ItemProperty : La propriété (Default) n'existe pas dans le chemin d'accè
s HKEY_CURRENT_USER\Software\Clients\StartMenuInternet\[Product Name].e
xe.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : InvalidArgument: ((Default):String) [Remove-Item
   Property], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException
   ,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand

Remove-ItemProperty : La propriété (Default) n'existe pas dans le chemin d'accè
s HKEY_CURRENT_USER\Software\Clients\StartMenuInternet\[Product Name].e
xe\DefaultIcon.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : InvalidArgument: ((Default):String) [Remove-Item
   Property], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException
   ,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand

Remove-ItemProperty : La propriété (Default) n'existe pas dans le chemin d'accè
s HKEY_CURRENT_USER\Software\Clients\StartMenuInternet\[Product Name].e
xe\shell\open\command.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : InvalidArgument: ((Default):String) [Remove-Item
   Property], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException
   ,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand

Remove-ItemProperty : La propriété (Default) n'existe pas dans le chemin d'accè
s HKEY_CURRENT_USER\Software\Classes\[Product Name]\DefaultIcon.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : InvalidArgument: ((Default):String) [Remove-Item
   Property], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException
   ,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand

Remove-ItemProperty : La propriété (Default) n'existe pas dans le chemin d'accè
s HKEY_CURRENT_USER\Software\Classes\[Product Name]\shell\open\comm
and.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : InvalidArgument: ((Default):String) [Remove-Item
   Property], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException
   ,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand

Remove-ItemProperty : La propriété (Default) n'existe pas dans le chemin d'accè
s HKEY_CURRENT_USER\Software\Classes\[Product Name]\DefaultIcon.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : InvalidArgument: ((Default):String) [Remove-Item
   Property], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException
   ,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand

Remove-ItemProperty : La propriété (Default) n'existe pas dans le chemin d'accè
s HKEY_CURRENT_USER\Software\Classes\[Product Name]\shell\open\comma
nd.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : InvalidArgument: ((Default):String) [Remove-Item
   Property], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException
   ,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand

Remove-ItemProperty : La propriété (Default) n'existe pas dans le chemin d'accè
s HKEY_CURRENT_USER\Software\Classes\Applications\[Product Name].exe\sh
ell\open\command.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : InvalidArgument: ((Default):String) [Remove-Item
   Property], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException
   ,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand

Remove-ItemProperty : La propriété (Default) n'existe pas dans le chemin d'accè
s HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\[Product Name].exe.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : InvalidArgument: ((Default):String) [Remove-Item
   Property], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException
   ,Microsoft.PowerShell.Commands.RemoveItemPropertyCommand

Remove-ItemProperty : Accès au registre demandé non autorisé.
Au niveau de C:\registre\registry_scriptv3.ps1 : 18 Caractère : 23
+             Remove-ItemProperty <<<<  -Path $Path -Name "$Name" -Force | out-
null
    + CategoryInfo          : PermissionDenied: (HKEY_CURRENT_US....htm\UserCh
   oice:String) [Remove-ItemProperty], SecurityException
    + FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.Powe
   rShell.Commands.RemoveItemPropertyCommand

New-ItemProperty : Accès au registre demandé non autorisé.
Au niveau de C:\registre\registry_scriptv3.ps1 : 20 Caractère : 19
+         New-ItemProperty <<<<  -Path $Path -Name "$Name" -Value $Value -Prope
rtyType $Type -ErrorAction Stop -Force | Out-Null
    + CategoryInfo          : PermissionDenied: (HKEY_CURRENT_US....htm\UserCh
   oice:String) [New-ItemProperty], SecurityException
    + FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.Powe
   rShell.Commands.NewItemPropertyCommand

I would really appreciate your help,

Thank you .

Awaiting for your reply

2
Hi, check the value of $registry, your CSV import may be the culprit. - sodawillow
The error occurs in line 15 at char 51, is this line part of your above sample? The last line of your sample won't work with no name etc. - user6811411

2 Answers

2
votes

First off all your path to the csv file is not correct! as stated by Randip. Second the last line in your csv file

"HKCU:\Software\Clients\StartMenuInternet\[ProductName].exe\Capabilities\FileAssociation","","",""

does not have a name/type/value so you can not create a itemproperty without a name!

here is a little code change I suggest:

$CsvFile = 'c:\listeRegistre.csv'



    import-csv -Path $CsvFile | ForEach-Object {
        $Name = $_.Name
        $Path = $_.Path
        $Type = $_.Type
        $Value = $_.Value
        IF(($_.Path -ne '') -and (!(Test-Path -Path $Path))) # If the registery path doesn't exist, create it
        {
            New-Item -Path $Path -Force | Out-Null
        }
        if (($Path -ne '') -and ($Name -ne ''))# if the property name is provided
        {
            if ((Get-ItemProperty -Path $Path -Name $Name) -ne $Null)
            {
                Remove-ItemProperty -Path $Path -Name "$Name" -Force | out-null

            }
            New-ItemProperty -Path $Path -Name "$Name" -Value $Value -PropertyType $Type -ErrorAction Stop -Force | Out-Null
        }
    }
0
votes

Its a bug in the input only. Please cross check that.

Instead of this:

$registries = import-csv "c:listeRegistre.csv"

Do This:

$registries = import-csv 'c:\listeRegistre.csv'

This should do your work.

Also check the output you are getting for each component inside the loop using write-host $registry and write-host $registryPath

Hope it helps.