Script Tip: Create multiple registry keys and values in Powershell

Doing some MP authoring in the lab, I was trying to write script to  create a series of registry keys on a number of systems in Powershell and I discovered I had a problem. On systems where the key or value exists, the script would abort and fail. So I did some reading and found Test-Path, a cmdlet that I could use to test for the existence of the key. With a simple If-then-else statement, that part was done.

But then, I found that even with the pre-check, the script failed if the key was there. So, I remembered the erroraction  parameter, which you can use to set the preferred behavior when an error is encountered. I think of it like the ON ERROR RESUME NEXT of Powershell. With erroraction silentlycontinue , I was able to create multiple keys, and skip all the errors that come up if the key or values already exists.

Sample Script

Here a sample that creates 1) a registry key 2) a child registry key beneath that one and 3) a String value beneath the child key.

Conclusion

Hope you find this helpful. Join me tomorrow when we talk ConfigMgr 2007!

Leave a Reply