# This function just gets $true or $falseįunction Test-RegistryValue($path, $name) $Regkey= “HKCU:\Software\Microsoft\Windows\CurrentVersion\PenWorkspace\” I wanted to write a script to change a Registry value in Powershell (deployed via Intune). If you have any questions, send email to me at or post your questions on the Official Scripting Guys Forum. I invite you to follow me on Twitter and Facebook. Troubleshooting Week will continue tomorrow when I will talk about more cool stuff. If you have such a need, you may want to check out this collection of Hey, Scripting Guy! Blog posts, where I talk about structured error handling in Windows PowerShell scripts.ĭC, that is all that is needed to fix your registry script. Therefore, I could implement structured error handling, detect the specific error, and then correct it as appropriate.īut that would be more complicated than I want to do for making simple registry changes. So, two different error messages are returned depending on the issue. The first error message complains about the PROPERTY, the second error complains about the PATH. FullyQualifiedErrorId : PathNotFound,圜ommand CategoryInfo : ObjectNotFound: (HKCU:\Software\ScriptingGuys\Scripts:String) , Get-ItemProperty : Cannot find path 'HKCU:\Software\ScriptingGuys\Scripts' because it does not exist. If the registry key itself does not exist, I get a different error message. FullyQualifiedErrorId :, tItemPropert圜ommand CategoryInfo : InvalidArgument: (version:String), PSArgumentException (Get-ItemProperty -Path HKCU:\Software\ScriptingGuys\Scripts -Name version).vers … Get-ItemProperty : Property version does not exist at path HKEY_CURRENT_USER\Software\ScriptingGuys\Scripts. If the registry key property does not exist, the error message is very specific. I could use Get-ItemProperty to verify if a registry key property value exists. Or, I can use the Get-ItemProperty cmdlet. I can use the Registry Editor to verify that my registry key property value exists with the proper value: As shown here, this code appears in the ELSE condition of the statement: If the registry key already exists, there is no need to attempt to create it again, so I create the registry key property value. New-Item -Path $registryPath -Force | Out-Null PS C:\> Test-Path HKCU:\Software\ScriptingGuys\scripts\version -PathType AnyĪfter I verify that the registry key exists, I use the New-ItemProperty cmdlet to create or update my registry key property value: PS C:\> Test-Path HKCU:\Software\ScriptingGuys\scripts\version -PathType Leaf PS C:\> (Get-ItemProperty -Path HKCU:\Software\ScriptingGuys\Scripts -Name version).version PS C:\> Test-Path HKCU:\Software\ScriptingGuys\scripts\version PS C:\> Test-Path HKCU:\Software\ScriptingGuys\scripts It will attempt to work, but it does not. The reason, is that Test-Path does not know how to work with registry key property values. After all, that is what the script is all about in the first place. You may wonder why I cannot use Test-Path to verify that the registry key property does not exist. I need to put the Test-Path statement in a pair of parentheses so that I am "NOTing" the condition. I use the explanation point ( ! ) as the not operator. To do this, I use the If statement, and I look for the registry key that is NOT existing. Now I can use the Test-Path cmdlet to see if the registry key exists. This consists of three variables as shown here: The first thing I like to do is to create the path to the registry key, then specify the property name and the value I want to assign. If the registry key does not exist, then you need to create the registry key, and then create the registry key property value. You need to test for the existence of the registry key. New-ItemProperty -Path $registryPath -Name $name -Value $value `Īnd here is the error message that appears when the registry key does not exist: $registryPath = "HKCU:\Software\ScriptingGuys\Scripts" When the registry key property exists, your script works. In your script, you are using the New-ItemProperty cmdlet to attempt to update a registry key property value. Microsoft Scripting Guy, Ed Wilson, is here. Is there something I am not doing? I am including my script so you can see what is going on. The error message says that it cannot find the path because it does not exist. I added the –Force parameter, but it still will not create the registry key. I am using the New-ItemProperty cmdlet, but it fails if the registry key does not exist. Hey, Scripting Guy! I am having a problem trying to update the registry. Summary : Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to update or add a registry key value.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |