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 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
Following the errors associated with my script :
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




$registry, your CSV import may be the culprit. - sodawillow