Well at least part of the problem is set-itemproperty. That assumes the subkey exists and already has a value assigned, which it doesn't cause you are creating the key, so you should be using new-itemproperty.
That should fix your missing subkey error message. I don't think it'll actually do what you want it to though. Because like I said it likely runs as SYSTEM, so you are injecting the subkey to S-1-5-18 as opposed to your end user's SID (HKCU is SYSTEM in this context). Since you are using set- you probably expect the subkey to exist in end user HKCU context.
I'm pretty sure there's a way easier way of doing this... I'm going to look at how we do Slack at work tomorrow.